Java object-oriented easy to forget points (1)

Source: Internet
Author: User
one, the object-oriented three major characteristics:

Encapsulation: not visible to external. Inheritance: Extends the functionality of classes. Polymorphism: Method overload, Object polymorphism. second, data type:

(1) General data type:

(2) Reference data type: Array, object .

A class belongs to a reference data type. Therefore, there is a reference relationship between stack memory and heap memory.

person p = null; Open Stack Memory
p = new Person ("John", 20); Open Heap Memory

The object is saved in the stack, the property is stored in the heap, and the method is saved in the Global code area, shared by all objects of the area's content. There are four kinds of memory areas in Java:


1. Stack memory: Save object.

2. Heap Memory: Save properties.

3. Global Data area: Save static type properties.

4. Global code Area: Save all method definitions.
code block: Code enclosed by {}

1. Common code block : a code block that is directly defined in a method.

2. Building block : Define the code block directly in the class.

The building block takes precedence over the constructor method, and the building block executes multiple times, and executes once as soon as one is instantiated.

3. Static code block : a block of code declared with the static keyword. takes precedence over the main method execution and is performed only once.

Functions: Initializing for static properties.

4. Sync code block: A block of code enclosed by synchronized.

The following procedures reveal the order in which these blocks of code are executed and the attributes:

Class demo{
	{
		System.out.println ("2, building Block"); 
	}
	static{
		System.out.println ("1, Static code block");
	Public Demo () {
		System.out.println ("3, Construct Method"); 
	}
public class codedemo01{
	static{
		System.out.println ("0, static code block for Main method"); 
	}
	public static void Main (String args[]) {
		demo D1 = new demo ();
		Demo D2 = new Demo ();
	}



v. Privatization of structural methods:

Characteristics:

(1) A class cannot be instantiated externally, but can be instantiated inside a class.

(2) can be generated internally by generating an object instance, which can then be transmitted externally.

Class singleton{
	private static Singleton s = new Singleton ();
	Private Singleton () {
		
	}
	public  void print () {
		System.out.println ("Hello World"); 
	}
	public static Singleton Getsingleton () {return
		s;
	}
}
Class singletondemo01{public
	static void Main (String args[]) {
		Singleton S1 = Singleton.getsingleton ();
		Singleton s2 = Singleton.getsingleton ();
		Singleton s3 = Singleton.getsingleton ();
		S1.print ();
		S2.print ();
		S3.print ();

	}


the meaning of the above procedure:

If you produce three objects of this class, but you have only one instantiated object, you can limit the objects that are instantiated.

In design mode, it is called a single State design pattern (single case design pattern singleton).

If you do not want a class to produce too many objects, use a single state design pattern, and you will encounter a Java class library that uses a single state design pattern. Like InetAddress.

The so-called single State is to restrict the instantiation of the object at the entrance.

For example, the Recycle Bin in Windows uses a single state design, there is a recycle Bin object on the desktop, there is a recycle Bin object on each hard disk, but in fact these objects have only one Recycle Bin instance.

A single state design pattern: Privatizing A class's construction method, then generating an instantiated object within the class and returning the reference to the instantiated object through the static method of the class.

six, the internal class:

1. Ability to access private properties of external classes.

2. However, the above internal class cannot be called directly from outside.

3. If an internal class uses the Static keyword declaration, the inner class is called a static inner class, similar to an external class. This allows direct access to the outside. External class. Internal class object = new Outer class. Internal class ();

Class outer{
	private static String info = "Hello";
	Static class inner{									//static inner class for externally visible public
		void print () {
			System.out.println (info);					Access private properties of external classes
		}
	} public
	void Fun () {
		new Inner (). print ();
	}
}
public class innerdemo03{public
	static void Main (String args[]) {
		new Outer.Inner (). print ();						Direct external access internal class
	}
}


4. If you want to access internal classes directly externally, you need the following syntax: External classes. Inner Class object = Outer class object. New inner class ();

Class outer{
	Private String info = "Hello";
	Class inner{public
		void print () {
			System.out.println (info); 	Access private properties of external classes
		}
	} public
	void Fun () {
		new Inner (). print ();
	}
}
public class innerdemo02{public
	static void Main (String args[]) {
		Outer o = new Outer ();
		O.fun ();
		Outer.Inner i = o.new Inner ();	External class. Internal class object = External class object. New inner Class ();
		I.print ();
	}


5. Define the inner class in the method: The parameter of the method must be the final keyword.

Class outer{
	Private String info = "Hello";
	
	public void Fun () {
		class inner{public
			void Fun (final int temp) {//inner class of declaration in method, parameter of method in this inner class must be final
				SYSTEM.OUT.PRINTLN (temp);
				SYSTEM.OUT.PRINTLN (info);
				
			}
		New Inner (). print ();
	}
public class innerdemo02{public
	static void Main (String args[]) {
		Outer o = new Outer ();
		O.fun ();
		Outer.Inner i = o.new Inner ();	External class. Internal class object = External class object. New inner Class ();
		I.print ();
	}


Note the point:

An instance of internal class B cannot be created in a static method when there is an outer class A containing internal class B;

Reason: The static method of an external class may not create an object of the outer class at the time of the call, but the inner class is created if the Outer class object has been created, so the inner class object cannot be created in the static method of the outer class;







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.