Java中的静态关键字

这个 静态关键字 在Java中主要用于内存管理。Java中的static关键字用于共享给定类的相同变量或方法。用户可以使用变量、方法、块和嵌套类应用静态关键字。static关键字属于该类,而不是该类的实例。static关键字用于常量变量或对类的每个实例都相同的方法。

null

这个 静止的 关键字是Java中的非访问修饰符,适用于以下情况:

  1. 阻碍
  2. 变量
  3. 方法
  4. 班级

注: 要创建静态成员(块、变量、方法、嵌套类),请在其声明之前加上关键字 静止的 .

当一个成员被声明为静态时,可以在创建其类的任何对象之前访问它,而无需引用任何对象。例如,在下面的java程序中,我们正在访问静态方法 m1() 不创建任何 测验

JAVA

// Java program to demonstrate that a static member
// can be accessed before instantiating a class
class Test
{
// static method
static void m1()
{
System.out.println( "from m1" );
}
public static void main(String[] args)
{
// calling m1 without creating
// any object of class Test
m1();
}
}


输出

from m1

静态块

如果需要进行计算以初始化 静态变量 ,可以声明一个静态块,该块在类首次加载时执行一次。

考虑下面的java程序演示静态块的使用。

JAVA

// Java program to demonstrate use of static blocks
class Test
{
// static variable
static int a = 10 ;
static int b;
// static block
static {
System.out.println( "Static block initialized." );
b = a * 4 ;
}
public static void main(String[] args)
{
System.out.println( "from main" );
System.out.println( "Value of a : " +a);
System.out.println( "Value of b : " +b);
}
}


输出

Static block initialized.
from main
Value of a : 10
Value of b : 40

有关静态块的详细文章,请参见 静态块

静态变量

当变量声明为静态时,将创建该变量的单个副本,并在类级别的所有对象之间共享。静态变量本质上是全局变量。该类的所有实例共享同一个静态变量。

静态变量的要点:

  • 我们只能在类级别创建静态变量。看见 在这里
  • 静态块和静态变量按照它们在程序中出现的顺序执行。

下面是一个Java程序,演示了静态块和静态变量在程序中的执行顺序。

JAVA

// Java program to demonstrate execution
// of static blocks and variables
class Test
{
// static variable
static int a = m1();
// static block
static {
System.out.println( "Inside static block" );
}
// static method
static int m1() {
System.out.println( "from m1" );
return 20 ;
}
// static method(main !!)
public static void main(String[] args)
{
System.out.println( "Value of a : " +a);
System.out.println( "from main" );
}
}


输出

from m1
Inside static block
Value of a : 20
from main

静态方法

当使用 静止的 关键字,它被称为静态方法。静态方法最常见的例子是 主要的() 方法如上所述,在创建其类的任何对象之前,可以访问任何静态成员,而无需引用任何对象。声明为静态的方法有几个限制:

  • 它们只能直接调用其他静态方法。
  • 他们只能直接访问静态数据。
  • 他们不能提及 超级的 无论如何。

下面是演示静态方法限制的java程序。

JAVA

// Java program to demonstrate restriction on static methods
class Test
{
// static variable
static int a = 10 ;
// instance variable
int b = 20 ;
// static method
static void m1()
{
a = 20 ;
System.out.println( "from m1" );
// Cannot make a static reference to the non-static field b
b = 10 ; // compilation error
// Cannot make a static reference to the
// non-static method m2() from the type Test
m2(); // compilation error
//  Cannot use super in a static context
System.out.println( super .a); // compiler error
}
// instance method
void m2()
{
System.out.println( "from m2" );
}
public static void main(String[] args)
{
// main method
}
}


输出:

prog.java:18: error: non-static variable b cannot be referenced from a static context
         b = 10; // compilation error
         ^
prog.java:22: error: non-static method m2() cannot be referenced from a static context
         m2();  // compilation error
         ^
prog.java:25: error: non-static variable super cannot be referenced from a static context
         System.out.println(super.a); // compiler error 
                            ^
prog.java:25: error: cannot find symbol
         System.out.println(super.a); // compiler error 
                                 ^
  symbol: variable a
4 errors

何时使用静态变量和方法?

对所有对象通用的特性使用静态变量。例如,在班级学生中,所有学生都使用相同的大学名称。使用静态方法更改静态变量。

考虑下面的java程序,说明了使用 静止的 带有变量和方法的关键字。

JAVA

// A java program to demonstrate use of
// static keyword with methods and variables
// Student class
class Student {
String name;
int rollNo;
// static variable
static String cllgName;
// static counter to set unique roll no
static int counter = 0 ;
public Student(String name)
{
this .name = name;
this .rollNo = setRollNo();
}
// getting unique rollNo
// through static variable(counter)
static int setRollNo()
{
counter++;
return counter;
}
// static method
static void setCllg(String name) { cllgName = name; }
// instance method
void getStudentInfo()
{
System.out.println( "name : " + this .name);
System.out.println( "rollNo : " + this .rollNo);
// accessing static variable
System.out.println( "cllgName : " + cllgName);
}
}
// Driver class
public class StaticDemo {
public static void main(String[] args)
{
// calling static method
// without instantiating Student class
Student.setCllg( "XYZ" );
Student s1 = new Student( "Alice" );
Student s2 = new Student( "Bob" );
s1.getStudentInfo();
s2.getStudentInfo();
}
}


输出

name : Alice
rollNo : 1
cllgName : XYZ
name : Bob
rollNo : 2
cllgName : XYZ

d

静态类

一节课可以上 静止的 仅当它是嵌套类时。我们不能用静态修饰符声明顶级类,但可以声明 嵌套类 静态的。这种类型的类称为嵌套静态类。嵌套的静态类不需要外部类的引用。在这种情况下,静态类无法访问外部类的非静态成员。

注: 有关静态嵌套类,请参见 java中的静态嵌套类

实施:

JAVA

// A java program to demonstrate use
// of static keyword with Classes
import java.io.*;
public class GFG {
private static String str = "GeeksforGeeks" ;
// Static class
static class MyNestedClass {
// non-static method
public void disp(){
System.out.println(str);
}
}
public static void main(String args[])
{
GFG.MyNestedClass obj
= new GFG.MyNestedClass();
obj.disp();
}
}


输出

GeeksforGeeks

本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享