Static in Java

Source: Internet
Author: User
Tags instance method

  • Static decorated member field: the static decorated member field is also called a class field or global field, and the static decorated member field is initialized when the class is loaded, associated with the class, and the static field exists as long as the class exists. A static field divides a single piece of storage space, which is not bound to a specific object, and is shared by the individual objects of the class. This means that when an object is declared, it does not produce a copy of the static field, but all instance objects of that class share the same static field. The static decorated member field can be accessed before any instance object is created, without referencing any object, that is, the static decorated field occupies only one piece of memory, regardless of how many objects are created.
    Class Statictest
    {
    static int i=47;
    }
    Static: For the properties of a class, for different objects Statictest st1=new statictest (); Statictest st2=new statictest (); There is only one shared storage space.
  • There are two ways to refer to a static field: You can use an object to locate it, or you can call it directly with its class name (preferably through the class name, so that the person looking at the program is not confused). If a member is declared static, it is able to be accessed before the object of its class is created, without referencing any object. The most common example is the main () method, where static-defined fields take precedence over any other non-static fields, regardless of the order in which they appear.
  • Java fields are divided into two classes at this level, class fields (static) and instance fields, and the instance fields cannot be accessed directly in a static method, and the instance fields need to be initialized to an instance of a class (object) before they can be used. The reason for this distinction is static to distinguish between classes and the members of an object. If a field or method is declared static, it means that the memory space must be allocated when the class is initialized. A static field belongs to a class, shared for all objects of that class, allocates memory space before the program starts executing, and if it is preceded by final, functions like a global constant, you cannot modify its value, such as Pi. Static fields cannot be defined in a static method.
  • The Java language can use the static keyword to decorate the class's member fields and member methods. A member field that is modified by static, which is generally referred to as a static (or global) field (static field), and is statically a method. Unlike general fields and methods, static fields, the scope of a static method is all objects of the class. That is, when run, in program space, all objects of a class are accessed to a static field that is the same value, and when one of the objects changes the value of a static field, other objects are affected. Because of this, static fields, static methods can (recommended) pass the class name. static field/class name. Static method Direct Access: 1, object. Field/Method: A member field that does not use the static modifier, a member method that acts on an object field, is often referred to as an instance field (instance field), Object methods (instance method). When an object is created, they really exist in the memory space, and the object itself changes to them without affecting other objects. It's like a person's object Zhangsan,zhangsan change does not affect other person objects. 2, Class. Field/Method: A member field that uses static adornments, a member method can be considered intrinsic to a class, and it acts on the entire object of the class. When all objects of a class need to share the same value, you can use static to define it. such as: pi=3.1415926, you can use static to define the PI field. Of course, this usually does not want to change the static field of the case, generally use static final to decorate.
  • If you need a static field that is initialized by a calculation, you can declare it as a static block, and the static block executes only once when the class is loaded.
  • A field declared as static is essentially a global field. When an object is declared, it does not produce a copy of the static field, but all instance fields of that class share the same static field. The method declared as static has the following limitations:
    • They can only invoke other static methods.
    • They can only access static data.
    • They cannot refer to this or super in any way
  • A field declared as static is initialized at the defined place and can only be initialized once. This means that if you define a static field in a method, the field is not initialized again when you call the method the second time.
  • Static method: A method that does not apply an action to an object, and a static method can be seen as a method without this parameter. You can use either the class name or the object to invoke a static method, and the class name is recommended. This is an implicit parameter that represents the object being constructed.
  • Routine: public class explicitstatic

{

public static void Main (string[] args)

{

SYSTEM.OUT.PRINTLN ("Inside Main ()");

CUPS.C1.F (99);

}

Static cups x=new cups ();

Static cups y=new cups ();

}

Class Cup

{

Cup (int marker)

{

System.out.println ("Cup (" +marker+ ")");

}

void f (int marker)

{

System.out.println ("F (" +marker+ ")");

}

}

Class cups{

Static Cup C1=new Cup (11);

static Cup C2;

Cup C3=new Cup (33);

Cup C4;

{

C3=new Cup (3);

C4=new Cup (4);

}

Static

{

C1=new Cup (1);

C2=new Cup (2);

}

Cups ()

{

System.out.println ("Cups ()");

}

}

Output:

Cup (11)

Cup (1)

Cup (2)

Cup (33)

Cup (3)

Cup (4)

Cups ()

Cup (33)

Cup (3)

Cup (4)

Cups ()

Inside Main ()

F (99)

The first initialization is static, the order is the code order Up-to-down, and then the method is called by itself.

The order of execution above is:

1:static cups x=new cups (); Transfer to Cups object

2:static Cup C1=new Cup (11); Transfer to cup object

3: The output cup of the Execution Cup (11);

The static Cup C2 in the 4:cups object;

5:static

{

C1=new Cup (1); Transfer to cup object Output cup (1)

C2=new Cup (2); Transfer to cup object Output cup (2)

}

The Cup C3=new Cup (33) in the 6:cups object; Output Cup (33)

7:c3=new Cup (3); Output Cup (3)

C4=new Cup (4); Output Cup (4)

8:cups ()

{

System.out.println ("Cups ()"); Output Cups ()

}

9:static cups y=new cups ();

Note that the static member in Cups has been initialized so

Static Cup C1=new Cup (11);

static Cup C2;

static{

C1=new Cup (1);

C2=new Cup (2);

}

No more execution!

The following is the same as above!

Static in Java

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.