Static, final modifier, internal class, staticfinal

Source: Internet
Author: User

Static, final modifier, internal class, staticfinal

Static modifier:
The static modifier can be used with attributes, methods, and internal classes to indicate static. Static variables and static methods in the class can be used together with the class name. You do not need to create a class object to access the static members of the class.

Class StaticText {// static variable static int I = 47; // static method static void echo () {System. out. println ("There is a Static Method");} // constructor StaticText () {System. out. println ("There is a Constructor");} // static domain static {System. out. println ("There is a static block") ;}} public class Text {public static void main (String [] args) {StaticText A = new StaticText (); staticText B = new StaticText (); //. I and B. I points to the same memory space System. out. println (. I = B. i);. I ++; // There is still. I = B. I System. out. println (B. i); // the preferred method for class name access, not only because it emphasizes the static structure of the variable, but also provides a better opportunity for Compiler optimization. out. println (StaticText. I );}}

The running result is:
There is a static block
There is a Constructor
There is a Constructor
True
True
48

Static attribute memory allocation: in a class, a static variable only has one memory space. Although there are multiple class instances, however, this static variable in these class instances will share the same memory space.

Static initiator-static block: a static block that exists outside the class method. It is executed only once during class loading and is usually used to initialize static class attributes.

Static variables are initialized when the class is loaded.

Static basic rules:

  • Static methods of a class can only access static properties;
  • Static methods of a class cannot directly call non-static methods;
  • For example, if the access control permission is permitted, static attributes and methods can be called by adding an object name and a ".", or by adding a "." to an instance;
  • The current object does not exist in the static method. Therefore, this and super cannot be used;
  • Static methods cannot be overwritten by non-static methods;
  • The constructor cannot be declared as static.

Although when static acts on a field, it will definitely change the data creation method (because the static field has only one bucket for each class, instead of static fields, each object has a bucket ). But when static acts on a method, the difference is not that big. An important role of the static method is to call it by class name without creating any object.

Final Modifier
When declaring classes, attributes, and methods in Java, you can use the keyword final to modify the Elements marked by final. The final component has the final state and indicates the final meaning.

Basic rules for the final modifier:

  • Classes marked by final cannot be inherited;
  • Methods marked by final cannot be overwritten by the quilt class;
  • Variables marked by final (member variables or local variables) become constants and can only be assigned once;
  • The member variables marked by final must be assigned a value at the same time as they are declared. If they are not assigned a value at the time of declaration, only one assignment is allowed. They can only be explicitly assigned in the constructor before they can be used;
  • Partial Variables marked by final can only declare that no value is assigned, and then assign values once;
  • Final is generally used to mark general functions, implementation methods, or components whose values cannot be changed at will to avoid misuse.

Internal class
Defines another class within a class (or method or statement block). The latter is called an internal class, or sometimes a nested class.

Features of internal classes:

  • Internal classes can reflect logical subordination, while other classes can control external visibility of internal classes;
  • The scope of the member variables of the external class is the entire external class, including the internal class, but the external class cannot access the private Members of the internal class;
  • Logically related classes can be combined to effectively implement information hiding. Internal classes can directly access members of external classes;
  • After compilation, the internal class is also compiled into a separate class named outclass $ inclass.
public class Outer{    private int size=0;    class Inner{      public int counter = 10;      public void doStuff(){          size++;      }    }    public static void main(String [] args){        Outer outer = new Outer();        Inner inner = outer.new Inner();        inner.doStuff();        System.out.println(outer.size);        System.out.println(inner.counter);       }   }

Internal classes can be divided into four types: 1. class level: member type with static modification; 2. Object level: member type, common, without static modification; 3. Local internal class: local formula; 4. Anonymous level: Local formula.

Static variables are stored in the method area.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.