zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    创建自动执行存储过程
    创建链接服务器
    SQLServer查询特殊符号处理
    SQL Server维护计划自动备份数据库
    SQL Server收缩数据库&列出所有表的数据条数
    SQL server日志文件过大处理方式
    SQL Server批量删除数据库表
    done apple经理面 匹配括号但是不能用stack
    done Beaconfire中国小哥中规中矩screening
    done marlabs挺难的screening
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825058.html
Copyright © 2011-2022 走看看