Java Classes and objects

Source: Internet
Author: User

Java Field Initialization rule:

Java field Initialization law: Variables of all classes are initialized to NULL by default, such as String A; Integer b;
The original data type of the numeric class is initialized to 0 by default, such as int A; Short B; char c;
Boolean is initialized to False by default; Java is initialized in two places: initialization blocks and constructors, where initialization blocks are divided into static initialization blocks and instance initialization blocks. Static initialization blocks are initialization blocks in a class that are decorated with static, and instance initialization blocks are initialization statements that do not have any keyword adornments in the class. If there is no formal parameter when creating an object in the main function, if a common variable is defined in the class and assigned, then the value is assigned to the variable in the main function, the default constructor in the class is called, and the corresponding constructor in the class is called if the object is created in the main function as a physical parameter.

Inherited:

The so-called "instantiate the object of the subclass before you instantiate the contents of the parent class, initialize for the parent class," refers to when the child class is being instantiated, always chaining calls to the parent class constructor method to initialize the parent class space. In other words, the constructor of a subclass must first call the constructor of the parent class before it can do any other initialization work.

public class jicheng{
public static void Main (string[] args) {
Stu s = new Stu ();
S.print ();
}
}
Class father{
String name;
{
Name= "Janny";
}
}
Class Son extends father{
int grade=;
void print ()
{
System.out.println ("Name:" +name+ "+" \ngrade: "+grade);
}
}

To statically access instance members:

The specific practice is to instantiate the class, get an instance variable, and pass the class as a parameter to the static method

static method (MyClass a) {//a Representation example variable
A.somemethod ();//Get a inside method
}

public class Ex
{
int i = 123;
static int Temp;

public static void A ()
{
System.out.println (Temp);
}

public int B ()
{
int x = 100;

return x;
}

public static void Main (stringargs[])
{
Ex e = new EX ();
Temp = e.b ();
E.a ();
}
}

After-school assignments:

public class A {
public static int count=0;
Public A ()
{
count++;
}
public static void Main (string[] args) {
A a=new a ();
A b=new a ();
SYSTEM.OUT.PRINTLN ("You have created the number of objects:" +a.count);
}
}

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.