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 }
标签:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· .NET周刊【4月第1期 2025-04-06】
· 国产的 Java Solon v3.2.0 发布(央企信创的优选)
· centos停服,迁移centos7.3系统到新搭建的openEuler
· 工良出品 | 长文讲解 MCP 和案例实战
· (原创)[开源][.Net Framework 4.0] SimpleLiveDataFeed(极易