[Java Basics] related to static keywords and javastatic keywords

Source: Internet
Author: User

[Java Basics] related to static keywords and javastatic keywords
Static keyword features:

  • Load as the class is loaded.
  • Takes precedence over object existence.
  • Shared by all objects
  • Can be called directly by class name.
Note:
  • Static methods can only access static members.
  • But non-static members can access static members.
  • This and super keywords cannot be used in static methods.
  • The main method is static.

After reading whether we want to go to bed, the concept is quite annoying. Let's deepen our understanding of them in actual code.

Methods can be called in two ways:

The method or field without static modification is an object. Is an instance Member.

The method or field modified using static is a class. Is a class member.

Call them:

If it is an instance Member: it can only be called through objects.

If it is a class member, it can be called by class name or object.

Okay, the following is a small program.

1 class dog // a group of dogs 2 {3 String name; 4 int age; // No static modifier is used, which belongs to the object and is an instance Member. 5 // the name of the dog, age. Each dog is different and can be changed at will. 6 7 static int num = 100; // use static modification. It belongs to the class and is a class member. 8 // The total number of dogs remains unchanged. 9} 10 11 class day5lesson2 12 {13 public static void main (String [] args) 14 {15 System. out. println ("calling class members by class name -->" + dog. num); // compiled through 16 // System. out. println ("calling instance members by class name -->" + dog. name); // compilation fails through 17 System. out. println ("calling class members through objects -->" + new dog (). num); // run through 18 System. out. println ("calling instance members through objects -->" + new dog (). name); // run through 19} 20}View Code

The error message for codes that fail to pass the comments in the above Code is as follows:

The above program also illustrates the above mentioned: if it is an instance Member: it can only be called through objects. If it is a class member, it can be called by class name or object. However, we recommend that you use a class name to call a class member, because at the underlying layer, objects call class members and convert them to class name calls.

 

The static area must be mentioned below. It is the storage area of a dedicated storage class member in the JVM memory.

Let's modify the main method of the above program:

1 class dog // a group of dogs 2 {3 String name; 4 int age; // No static modifier is used, which belongs to the object and is an instance Member. 5 // the name of the dog, age. Each dog is different and can be changed at will. 6 7 static int num = 100; // use static modification. It belongs to the class and is a class member. 8 // The total number of dogs remains unchanged. 9} 10 class day5lesson2 11 {12 public static void main (String [] args) 13 {14 dog d1 = new dog (); 15 dog d2 = new dog (); // declare two objects 16 17 d1.num = 111; // use d1 to modify class members. 18 System. out. println ("d1.num =" + d1.num); 19 System. out. println ("d2.num =" + d2.num); // output 20 21 d1.age = 111; // modify the instance Member with d1. 22 System. out. println ("d1.age =" + d1.age); 23 System. out. println ("d2.age =" + d2.age); // output 24} 25}View Code

The result is as follows:

Why is this? This is where the static modified field belongs to the class member and is stored in a special static area. (PS: creates space for a class when it is loaded, so it consumes a lot of performance ). Generally, static methods are used as tool-class methods. For example, in the Arrays class (java. util. Arrays), static methods are widely used and can be viewed in JDK APIs.

The age (instance Member) without static modification does not have this effect. (PS: int defines a number. The default value is 0, so d2.age outputs 0)


Static keyword in JAVA

Static is a java keyword and also a keyword for static modification. variables or methods modified using this keyword can be called directly through the class name, for example:
Public class {
Static int a = 10;
}
Class B {
Int B = a. a; // because the integer variable of Class a is modified using static, it can be called by class name.
}

One advantage of this is that you do not need to create a new class, which can save a lot of space for the memory. Variables modified by static are called member variables and member variables are called global variables, the static method is called the member method.

Significance of the static keyword in java

This is a bit like the concept of "global.

Public class MyClass {
Public static void aStaticMethod () {...}; // static method
Public void aNonStaticMethod () {...}; // non-static method
...
}
When you define a class, if the method or variable in it is defined as static, then this method or function only has one object for this class, so it can be referenced and accessed using the class name, for example, MyClass. aStaticMethod ()

Instead of static methods or variables, you must create a new object for this class. For example, the non-static method above must use new MyClass (). aNonStaticMethod ().

This is easy to understand.
For example, if you buy a car of a certain brand, your car should create an object for the car of this brand. It should be a non-static method for you to drive and repair your own car.
The car repair shops on the street are public for all the cars of your brand. Their Car Repair and car washing methods should be static.

In addition, there are some rules. For example, static methods cannot contain non-static methods, but static methods can be used in non-static methods. In this case, I will learn more about them.

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.