Basic learning of Java classes and objects

Source: Internet
Author: User

1.

Please enter and run the following code to get what results?

public class test{

public static void Main (String args[]) {

Foo obj1=new foo ();

Foo obj2=new foo (); System.out.println (OBJ1==OBJ2);

}

}

Class Foo

{int value=100;}

Results:

Cause: when "= =" is applied to the original data type variable, is the data stored by the comparison variable equal; when the "= =" is applied to a reference type variable, it is compared whether the two variables refer to the same object; The reference represents the address, so "= =" is actually equivalent to comparing the address of an object saved in two reference type variables.

2.

Why is the following code not compiled? Where did it go wrong?

public class test{

public static void Main (string[] args) {

Foo obj1=new foo ();

}

}

Class Foo {

int value;

Public Foo (int initvalue) {

Value =initvalue;

}

}

Cause: The class provides a constructor method, so the system defaults to No.

3. Using the class defined on the previous page slide, what is the output of the following code? According to the output of the code, summarize the law of Java field initialization.

(1) The default value specified when executing the class member definition or the initialization block of the class, exactly which one you want to see is "in front".

(2) Executes the constructor of a class, which is distinguished by a parameter when there are multiple constructors.

4. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block. Please write your own sample code to verify the above conclusions.

When there are inheritance relationships between multiple classes, creating a subclass object causes

The parent class initializes the execution of the block.

Package demo;

Class Class1

{

Static

{

SYSTEM.OUT.PRINTLN ("Static initialization block of Class 1");

}

}

Class Class2 extends Class1

{

Static

{

SYSTEM.OUT.PRINTLN ("Static initialization block of Class 2");

}

}

Class CLASS3 extends Class2

{

Static

{

SYSTEM.OUT.PRINTLN ("Static initialization block of Class 3");

}

}

public class GFS

{

public static void Main (string[] args)

{

New CLASS3 ();

}

}

Results:

5. Run the Teststaticinitializeblock.java sample, observe the output, and summarize the "Order of execution of static initialization blocks".

(1) Static initialization blocks are executed only once.

(2) When creating objects of subtypes, the execution of static initialization blocks of the parent type is also caused.

6. Static methods allow access to only static data, so how do you access instance members of a class in a static method (that is, a field or method that does not have a static keyword attached)? write code to verify your idea.

public class test{
int a = 2;//instance variable
static int b = Statically variable of 3;//class
public static void variable ()//static method
{
SYSTEM.OUT.PRINTLN ("instance variable a =" + New Test (). a);
Accessing an instance variable of a class in a static method requires first instantiating the class

SYSTEM.OUT.PRINTLN ("static variable B =" + B);
Static variables of a class can be accessed directly in a static method
}
public static void Main (string[] args)
{
Test.variable ();
Test test = new test ();
SYSTEM.OUT.PRINTLN ("Result: Instance variable =" + test.a);//Access Instance member
}
}

7. Magic Code.

public class Strangeintegerbehavior

{

public static void Main (string[] args)

{

Integer i1=100;

Integer j1=100;

System.out.println (I1==J1);

Integer i2=129;

Integer j2=129;

System.out.println (I2==J2);

}

8. Use the class's static fields and constructors to track the number of objects that a class has created.

Code:

Package demo;

public class Search {
static int i;
Public Search ()
{
i++; Every object that creates a class I plus 1
}
static int Getnum ()
{
return i;
}

public static void Main (string[] args) {
Search S1 = new search ();
Search S2 = new search ();
System.out.println ("Number of objects created:" +search.getnum ());
}

}

Basic learning of Java classes and objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.