Guidelines for using static in Java _java

Source: Internet
Author: User
Tags modifier

Static variables for static use in Java

The members that are decorated by static in 1.Java are called static members or class members. It belongs to the entire class, not to an object, which is shared by all objects of the class, and takes precedence over object existence. Static members can be accessed directly by using the class name, or by using the object name. You can use static to decorate variables, methods, and blocks of code.

The 2.public modifier represents a public, publicly owned, static variable that uses the static modifier

3. Static methods can call static members of the same class directly, but not non-static members directly.

public class hellworld{
String name = "Java";//non-static variable static
String hobby = "programing";//static
variable PU Blic static void print () {
System.out.println ("Welcome:" + name + "!"); /cannot directly call Non-static variable
System.out.println ("Welcome:" + Hobby + "!"); /can directly call static variables
}
}

4. If you want to call a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object.

public class hellworld{
string name = "Java";//non-static variable static
String hobby = "program";//static variable
//static side      method to call the non-static variable public
static void print () {
/////Create the object of the class
HelloWorld hello=new HelloWorld ();
The object is implemented to call non-static variables in static methods
System.out.println ("Welcome:" +hello.name+ ");
Static methods can be directly called static variables
System.out.prinltn ("Welcome like" +program+ "" +hello.name);
}
}

5. In the ordinary member method, you can directly access the same kind of non-static variables and static variables

public class hellworld{
string name = "Java";//non-static variable static
String hobby = "programing";//static
variable      public void print () {//Common method
System.out.println ("Welcome:" + name + "!");
SYSTEM.OUT.PRINLTN ("Welcome like" +program+ "+hello.name);
}
}

6. Static methods cannot call non-static methods directly and need to access non-static methods through objects

public class hellworld{
String name = "Java";//non-static variable static
String hobby = "program";//static variable
//non-static Method public
void Show () {
System.out.println ("I am a non-static method and cannot be invoked directly by a static method ...")    ");
}
static method public
static void Show2 () {
System.out.println ("I am a static method that can be called directly by a static method");
A non-static
method is invoked through an object in a static method.      You can directly call the static method public
static void print () {
////Create the object of the class
HelloWorld hello=new HelloWorld ();
An object is implemented to invoke a Non-static method Hello.show () in a static method
.
static method Show2 () can be directly invoked in static methods
.
}
}

Static initialization blocks for static use in Java

1. In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when an instance of the class is created. If you use the static modifier to initialize the block, it is called a static initialization block.

2. The difference between an instance variable and a class variable:

A) Location: The class variable is stored in the method area as the class is loaded, and the instance variable is stored in the heap memory as the object is built.

B 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.

3. Static initialization blocks are executed only once when the class is loaded, and only once, while static initialization blocks can only assign values to static variables and cannot initialize ordinary member variables. When the program runs, the static initialization is executed quickly first and takes precedence over the main function, then executes the normal initialization block before executing the construction method.

public class Staticdemo {
int num1;//declaring variable 1
int num2;//declaring variable 2
static int num3;//declaring static variable 3 public
Stati    CDemo () {//construction method
NUM1 =;
System.out.println ("Assignment of variable 1 by construction method");
}
{//initialization block
num2 =;
SYSTEM.OUT.PRINTLN ("Assign value by initialization block to Variable 2");
static{//Static Initialization This cannot be giel ordinary variable assignment
num3 =;
SYSTEM.OUT.PRINTLN ("Assign value by static initialization block to static variable 3");
Public
static void Main (string[] args) {
Staticdemo hello = new Staticdemo ()//object to create class Hello
Syst    Em.out.println ("NUM1:" + hello.num1);
System.out.println ("num2:" + hello.num2);
System.out.println ("num3:" + hello.num3);
Staticdemo hello1 = new Staticdemo ();
}
}

Run Result:

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.