Java Access Modifiers – 공개, 보호, 비공개 및 기본 노트
//static variable example
private static int count;
public static String str;
public static final String DB_USER = "myuser";
2 . Java static methods: Same as static variables, static methods belong to class and not to class instances. A static method can access only static variables of class and invoke only static methods of the class. Usually static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance; for example Collections class. Java Wrapper classes and utility classes contains a lot of static methods. The main() method that is the entry point of a java program itself is a static method.
//static method example
public static void setCount(int count) {
if(count > 0)
StaticExample.count = count;
}
//static util method
public static int addInts(int i, int...js){
int sum=i;
for(int x : js) sum+=x;
return sum;
}
From Java 8 onwards, we can have static methods in interfaces too, for more details please readJava interface static methods.
3 .Java static Block: Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it’s used to create static resources when class is loaded. We can’t access non-static variables in static block. We can have multiple static blocks in a class, although it doesn’t make much sense. Static block code is executed only once when class is loaded into memory.
static{
//can be used to initialize resources when class is loaded
System.out.println("StaticExample static block");
//can access only static variables and methods
str="Test";
setCount(2);
}
4 . Java Static Class: We can use static keyword with nested classes. static keyword can’t be used with top-level classes. Static nested class is same as any other top-level class and is nested for only packaging convenience.Read: Java Nested Classes
발췌:http://www.journaldev.com/7153/java-101-tutorials-learn-java
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.