Академический Документы
Профессиональный Документы
Культура Документы
1) static keyword can be applied with variable, method or nested class. static keyword can not be applied on
top level class. Making a top level class static in Java will result in compile time error.
2) static variables are associated with class instead of object.
3) static variables in java keeps same value for every single object.
4) you can not use non-static variable inside a static method , it will result in compilation error as shown below.
See Why static variable can not be called from static method for more details.
public class TradingSystem {
String description = "electronic trading system";
public static void main(String[] args) {
description = "commodity trading system";
}
}
Cannot make a static reference to the non-static field description
at TradingSystem.main(TradingSystem.java:8)
6) Static fields are initialized at the time of class loading in Java, opposite to instance variable which is initialised
when you create instance of a particular class.
7) Static keyword can also be used to create static block in Java which holds piece of code to executed when class
is loaded in Java. This is also known as static initialize block as shown in below example.
static {
String category = "electronic trading system";
System.out.println("example of static block in java");
}
8) Static method can not be overridden in Java as they belong to class and not to object. so if you have same
static method in subclass and super class , method will be invoked based on declared type of object instead of
runtime for example. Can we override static method in Java is also a popular Java question asked in interviews.
This shows that static method can not be overridden in Java declaring same static
method on Child class is known as method hiding in Java.
9. If you try to override a static method with a non-static method in sub class you will get compilation error.
10. you can overload static method in Java but you can not override static method in Java.
**
* Java program to show that we can overload static method in Java.
*/
public class StaticOverloadingTest {
public static void main(String args[]) {
greet("John"); //will call static method with one String argument
greet("John", "Good Morning"); //overloaded static method will be call
}
/*
* static method which will be overloaded
*/
public static void greet(String name){
System.out.println("Hello " + name);
}
/*
* Another static method which overload above Hello method
* This shows that we can overload static method in Java
*/
public static void greet(String name, String greeting){
System.out.println(greeting + " " + name);
}
}
Output
Hello John
Good Morning John
static is a keyword in java and we cant create a class or package name as static. Java
static keyword can be used in four cases.
1.
Java static variables: We can use static keyword with a class level
variable. A static variable is a class variable and doesnt belong to
Object/instance of the class. Since static variables are shared across all the
instances of Object, they are not thread safe. Usually static variables are
used with final keyword for common resources or constants that can be used
by all the objects. If the static variable is not private, we can access it
with ClassName.variableName
1
//static variable example
private static int count;
2
public static String str;
3
public static final String DB_USER = "myuser";
4
2.
3.
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 read Java interface static methods.
4.
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 its used to create static
resources when class is loaded. We cant access non-static variables in static
block. We can have multiple static blocks in a class, although it doesnt
make much sense. Static block code is executed only once when class is
loaded into memory.
1
static{
2
//can be used to initialize resources when class is loaded
3
System.out.println("StaticExample static block");
//can access only static variables and methods
4
str="Test";
5
setCount(2);
6
}
7
5.
Java Static Class: We can use static keyword with nested classes. static
keyword cant be used with top-level classes. Static nested class is same as
any other top-level class and is nested for only packaging convenience.
import java.util.Date;
/**
* Java program to show how to call non static method from static method in Java
*
* @author http://java67.blogspot.com
*/
public class StaticTest {
Output:
Tue Nov 06 19:07:54 IST 2012
So these were some of the frequently asked questions about main method in Java. This are not only help to answer
interview question but also to build your concept on main method in Java.