• Java Class Variable/Static Variable


    1. Class Variable/Static Variable:

    • Class variable is also known as static variable with "static" keyword inside the class but outside the methods.
    • There is only one copy of class variable is no matter how many objects are initiated from this class.
    • Class variable is accessed as: className.classVariableName.
    • Static variable is pretty like constant, declared with key word "static", stored in static memory, created when program begins and destroyed when program ends.

    2. Java Static Method:

    • A static method belongs to a class rather than a object.
    • A static method could be invoked without creating an object.
    • Static method could access and change static data value.

    3. Static Block:

    • It is used to initialize static data member.
    • It is executed before main method at the time of class loading.

    4. Static Class:

    • There is inner class nested in outer class, inner class could be static class, out class can't be static class.
    • Inner static class doesn't need reference of outer class, but inner non-static class need reference of outer class.
    • Inner static class could only access outer static members of outer class.
    •  1 class OuterClass{
       2    private static String msg = "GeeksForGeeks";
       3     
       4    // Static nested class
       5    public static class NestedStaticClass{
       6       
       7        // Only static members of Outer class is directly accessible in nested 
       8        // static class 
       9        public void printMessage() {
      10  
      11          // Try making 'message' a non-static variable, there will be 
      12          // compiler error  
      13          System.out.println("Message from nested static class: " + msg); 
      14        }
      15     }
      16     
      17     // non-static nested class - also called Inner class
      18     public class InnerClass{
      19         
      20        // Both static and non-static members of Outer class are accessible in 
      21        // this Inner class
      22        public void display(){
      23           System.out.println("Message from non-static nested class: "+ msg);
      24        }
      25     }
      26 } 
      27 class Main
      28 {
      29     // How to create instance of static and non static nested class?
      30     public static void main(String args[]){
      31         
      32        // create instance of nested Static class
      33        OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();
      34         
      35        // call non static method of nested static class
      36        printer.printMessage();   
      37   
      38        // In order to create instance of Inner class we need an Outer class 
      39        // instance. Let us create Outer class instance for creating 
      40        // non-static nested class
      41        OuterClass outer = new OuterClass();        
      42        OuterClass.InnerClass inner  = outer.new InnerClass();
      43         
      44        // calling non-static method of Inner class
      45        inner.display();
      46         
      47        // we can also combine above steps in one step to create instance of 
      48        // Inner class
      49        OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
      50         
      51        // similarly we can now call Inner class method
      52        innerObject.display();
      53     }
      54 }
  • 相关阅读:
    Asp.net实现URL重写
    IHttpModule不起作用的两个原因
    从客户端中检测到有潜在危险的 request.form值[解决方法]
    PHP $_SERVER详解
    string.Format 格式化日期格式
    图解正向代理、反向代理、透明代理
    Javacard 解释器怎样在API类库中找到源文件调用的类、方法或者静态域?
    API
    指令集
    机器码与字节码
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825058.html
Copyright © 2020-2023  润新知