The static class in Java

Source: Internet
Author: User

Can a class in Java be static? The answer is yes. In Java we can have static instance variables, static methods, static blocks. The class can also be static.

Java allows us to define static classes within a class. such as the inner class (nested classes). The class enclosing the nested class is called an external class. In Java, we cannot decorate the top level class with Static. Only the inner class can be static.

What is the difference between a static inner class and a non-static inner class? Here are the main differences between the two.

(1) Internal static classes do not need to have references to external classes. But non-static inner classes need to hold references to the external class.

(2) non-static inner classes can access static and non-static members of external classes. Static classes cannot access non-static members of external classes. He can only access static members of external classes.

(3) A non-static inner class cannot be created from an external class entity, and a non-static inner class can access the data and methods of the external class, because he is inside the outer class.

(4) Static inner classes can declare ordinary member variables and methods, while ordinary inner classes cannot declare static member variables and methods.

Static inner classes can be initialized individually:

Inner i = new Outer.Inner ();

Normal internal class initialization:

Outer o = new Outer (); Inner i = O.new Inner ();

Based on the above discussion, we can make programming simpler and more efficient through these features.

/* The following program demonstrates how to create a static inner class and a non-static inner class in Java */class outerclass{private static String msg = "Geeksforgeeks";  Static internal class public static class nestedstaticclass{//Static inner class can only access static members of external classes public void Printmessage () {//        Try to change msg to non-static, which will result in compilation error System.out.println ("Message from nested static class:" + msg);          }}//non-static inner class public classes innerclass{//both static and non-static methods can access public void display () in non-static inner classes {       System.out.println ("Message from Non-static nested class:" + msg); }}}} class main{//How to create an instance of a static inner class and a non-static inner class public static void Main (String args[]) {//Create an instance of a static inner class OUTERCL The.       Nestedstaticclass printer = new Outerclass.nestedstaticclass ();          Create a static internal class non-static method Printer.printmessage ();               In order to create a non-static inner class, we need an instance of the outer class outerclass outer = new Outerclass ();       Outerclass.innerclass inner = outer.new innerclass ();       A non-static method called Inner.display () that calls a non-static inner class; We can also combine the above steps to create an internal class instance in one step OuteRclass.innerclass innerobject = new Outerclass (). New Innerclass ();    Similarly we can now invoke the inner class method Innerobject.display (); }}

A static inner class usage scenario is typically when an external class needs to use an inner class, and the inner class does not need an external class resource, and the inner class can be created separately , taking into account the design of the static inner class, knowing how to initialize the static inner class in the effective Java The static internal class builder described in Chapter II describes how to use static inner classes:

public class Outer {    private String name;    private int age;    public static class Builder {        private String name;        private int age;        Public Builder (int.) {            this.age = age;        }        Public Builder withname (String name) {            this.name = name;            return this;        }        Public Builder Withage (int.) {            this.age = age;            return this;        }        Public Outer Build () {            return to new Outer (this);}    }    Private Outer (Builder b) {        this.age = b.age;        THIS.name = B.name;    }}

The static inner class calls the constructor of the outer class to construct the outer class, since the static inner class can be initialized by itself and there is an external implementation of the following:

Public Outer Getouter () {    Outer Outer = new Outer.builder (2). Withname ("Yang Liu"). Build ();    return outer;}

For the static class summary is: 1. If you have multiple parameters in a class's constructor or static factory, it is best to use the builder mode when designing such a class, especially if most of the parameters are optional.

2. If you cannot determine the number of parameters now, it is a good idea to use the builder as Builder mode at the beginning.

Reference links

Http://stackoverflow.com/questions/7486012/static-classes-in-java

The static class in Java

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.