java--object-oriented (Static keyword, main, static when used, static code block, object initialization process)

Source: Internet
Author: User

One: Static key word:

Package A;


Class person{
	String name;
	String country= "CN";
	public void Show () {
		System.out.println (name+ "::" +country);
	}
}
Public  class a{public
	
	static void Main (String [] args)
	{person
		p=new person ();
		P.name= "Zhangsan";
		P.show ();
	}

Console output results:
ZHANGSAN::CN

/
 * Static: Static
 * Usage: is a modifier for the monk member (member variable, member function).
 * Party members have been statically decorated, there is more than one way to invoke, in addition to the object can be invoked,
 * can also be directly called by the class name, the format is:  class name. static member//

Sample one:

package A;
Class person{
	String name;
	Static String country= "CN";
	public void Show () {
		System.out.println (name+ "::" +country);
	}
}
Public  class a{public
	
	static void Main (String [] args)
	{person
		p=new person ();
		System.out.println (p.country);
		
		System.out.println (person.country);
	}
/*
 console output:
 CN
  
 *


//Sample II:/
*
Package A;
Class person{
	string name;//member variable, instance variable
	static string country= "CN";//static member variable, class variable public
	void show () {
		System.out.println (name+ "::" +country);
	}
Public  class a{public
	
	static void Main (String [] args)
	{
	   System.out.println (person.country);
	}
}

Console output:
CN */
 


Static Features:

1. Loading as class is loaded

That is, the static disappears with the class, which means its life cycle is the longest.

2. Precedence over object existence

Specifically, the static implementation exists, and the object is 1 after the existence.

3. Being shared by the object

4. Can be called directly by the class name


The difference between an instance variable and a class variable:

1. Storage location: Class variables exist in the method area as the class is loaded, and the instance object exists in heap memory as the class is built.

2. Life cycle: The life cycle of the class variable is the longest and disappears as the class disappears; the instance variable lifecycle disappears as the object disappears.


Static usage Considerations:

1. Static methods can only access static members.

Non-static methods can access both static members and Non-static members

2. Static methods can define the This,super keyword

Because static takes precedence over object existence, this is not possible in static methods

(ii) Main:

/*
public static void Main (String [] args)


Main function: The main function is a special function, as the entrance to the program,


The definition of the main function:
Public: Represents the function with the maximum access rights
Static: Represents a function that already exists as the class is loaded
void: The main function does not have a specific return value
Main: Not a keyword, but a special word that can be identified
The main function is a fixed form,
*/


(iii) When to use static:

/*
* What to use static.
* To start from two aspects:
* Because statically decorated with member variables and functions.
* When to use static.
* When shared data is present in the object, (not attributes, such as two people share the name of John, which belongs to the attribute), the school belongs to the shared data, the data is modified statically.
* The unique data in the object is defined as non-static in heap memory.
* When do I define a static function?
* When there is no access inside the function
*/

Static Application--tool class:

First create a Class A, then define the main function, then create a tool Class AA, and then run a.

Package A;

public class a{public
	static void Main (string[] args)
	{
		int s=aa.max ();
		System.out.println ("max=" +s);
	}

Console run Result:
max=8


//aa Tool class:
package A;

public Class AA 
{public
              static int max ()
              {
            	  int[] arr={2,1,3,4,6,5,8,7,0};
            	  int max=0;
            	  for (int i=0;i<arr.length-1;i++)
            	  {
            		  if (Arr[i]>arr[max])
            		  {
            			  max=i
            		  }
                  } return Arr[max];
              }


() Static code block:

static{

Execution statement in a static code block.

}

Features: Loaded with class loading, executed only once, takes precedence over the main function execution, and is used to initialize the class.

As follows: first create a Class A, and then create another Class AA.

package A; public class a{public static void Main (String [] args) {new AA ();
		System.out.println ("over");
		} static {System.out.println ("B");
		} static {System.out.println ("a");

}/* Console output Result: b a SS over * * Package A;
             public Class AA {static{System.out.println ("ss"); }
}
Related Article

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.