Java Static class declaration--java class can be declared as static

Source: Internet
Author: User

Original: http://www.javaworld.com/article/2077372/learn-java/static-class-declarations.html

To understand the use of the static keyword in a class declaration, we first need to understand the class declaration. There are two kinds, one is top-level class, the other is inner class.

Top-level classes

The top-level class can be declared as a package member, and each top-level class corresponds to a Java file with the same file name as the class name.

Because the top-level class is already top-level, there is no need to declare it as static. If the top-level class is declared as static, the compiler will give an error.

Inner classes

The inner class can be defined in the top-level class, and the inner class can have the following four forms, depending on how the inner class is defined:

1. Anonymous: The Declaration and initialization of an anonymous class is in the same statement. Anonymous classes do not have a class name and can only be instantiated once. Examples are as follows:
Okbutton.addactionlistener (new ActionListener () {public   void actionperformed (ActionEvent e) {      Dispose ();   }});

Because anonymous classes do not have a standard class declaration, they cannot be declared as static.

2. Local.

A local class is like a local variable, and it is created and used in a block of code. Once you declare a class in a block of code, you can instantiate the class multiple times in this block of code. Just like a local variable, a local class cannot be declared as public,private,protected or static.

Examples are as follows:

Some code block .... {   class Listlistener implements ItemListener {      list list;      Public Listlistener (list l) {         list = l;      }      public void itemstatechanged (ItemEvent e) {         String s = l.getitemselected ();         DoSomething (s);      }   }   List list1 = new list ();   List list2 = new list ();   List1.additemlistener (New Listlistener (List1));   List2.additemlistener (New Listlistener (List2));}
3. Member.

Member classes are defined in a different class. In the class that contains the member class, the member class can be used in any place.

The member inner class is the only one that can be declared as a static class, and when the member class is declared static, it becomes the top-level class.

Examples are as follows:

//external classes class out         {private int age = 12;        Inner class in {public void print () {System.out.println (age);        }}} public class Demo {public static void main (string[] args) {out.in in = new Out (). New in ();        In.print ();        or Access/* out with sowing = new out ();        Out.in in = Out.new in ();        In.print (); */    }}
Class out {    private int: age = n;         Class in {        private int: age =;        public void print () {            INT-age = +;            SYSTEM.OUT.PRINTLN ("local variable:" + age);            System.out.println ("Inner class variable:" + this.age);            SYSTEM.OUT.PRINTLN ("External class variable:" + Out.this.age);}}}    public class Demo {public    static void Main (string[] args) {        out.in in = new Out (). New in ();        In.print ();    }}
Inner class without a member variable and local variable of the same name, the inner class accesses the member variable of the outer class directly without specifying Out.this. Property name

Otherwise, local variables in the inner class will overwrite the member variables of the outer class.

4. Nested top-level.

The nested top-level class is a member class with the static modifier. The netsted top-level class makes it easy to organize related classes together without having to create new packages.

Examples are as follows:

public class Filter {   vector criteria = new vector ();   Public addcriterion (Criterion c) {      criteria.addelement (c);   }   public boolean isTrue (Record rec) {for      (enumeration e=criteria.elements ();      E.hasmoreelements ();) {         if (! ((Criterion) e.nextelement ()). IsTrue (REC))             return false;      return true;   }   public static class Criterion {      String colname, Colvalue;      Public Criterion (stirng name, String val) {         colname = name; colvalue = val;      }      public boolean isTrue (Record rec) {         String data = Rec.getdata (colname);         if (Data.equals (Colvalue)) return true;         return false;}}   } Filter F = new filter (), F.addcriterion (New Filter.criterion ("SYMBOL", "SUNW")), F.addcriterion (New Filter.criterion (" SIDE "," BUY ")), ... if (F.istrue (SOMEREC))//do some thing .....

As you can see, if you statically internal static, the inner class can only access static member variables of the outer class, with limitations

Second, because the inner class is statically, the out.in can be viewed as a whole, and can be directly new to the object of the inner class (access to static through the class name, it doesn't matter if the external class object is generated)





Java Static class declaration--java class can be declared as static

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.