37
loading...
This website collects cookies to deliver better user experience
public static void main(String [] args) {
//line of code
//line of code....
}
public class Students {
static String schoolName = "ABC Public School";
public static void main(String [] args) {
System.out.println("Name of school is " +schoolName);
}
}
Students.schoolName;
public class Students {
static void calculatePercentage() {
// this is a static method
}
void calculatePercentage() {
// this is a non-static method
}
}
Students st = new Students();
st.calculatePercentage();
Students.calculatePercentage();
Why is the main method static?
Because program execution begins from it, and no object exists before calling it. This save lots of memory by JVM.
import java.util.Date;
public class Students {
static {
Date date = new Date();
System.out.println("Today's date is :" + date);
}
public static void main(String [] args) {
System.out.println("Hello");
}
}
public class Students {
static {
System.out.println("Hello this is first static block");
}
static {
System.out.println("Hello this is second static block");
}
static {
System.out.println("Hello this is third static block");
}
public static void main(String [] args) {
System.out.println("Hello this is inside main");
}
}
public class Students {
class Marks {
}
}
public class Students {
static String schoolName = "ABC Public School";
static class Inner {
void name() {
System.out.println("School name is "+ schoolName);
}
}
public static void main(String [] args) {
Students.Inner st = new Students.Inner();
st.name();
}
}