/*
* A static method can't make reference of a non-static variable.
* But a non-static method can make reference of a non-static as well as static variable.
*/
package SCJP;
public class StaticMethodExample
{
static int static_variable = 99;
int non_static_variable = 98;
public static void static_method()
{
System.out.println("Access static variable from static method = "+static_variable);
/*System.out.println("Access non-static variable from static method = "+non_static_variable); can't do this coz of non-static variable.*/
}
public void non_static_method()
{
System.out.println("Access static variable from non-static method = "+static_variable);
System.out.println("Access non-static variable from non-static method = "+non_static_variable);
}
public static void main(String[] arun)
{
new StaticMethodExample().static_method();
new StaticMethodExample().non_static_method();
}
}
Output =>
Access static variable from static method = 99
Access static variable from non-static method = 99
Access non-static variable from non-static method = 98
* A static method can't make reference of a non-static variable.
* But a non-static method can make reference of a non-static as well as static variable.
*/
package SCJP;
public class StaticMethodExample
{
static int static_variable = 99;
int non_static_variable = 98;
public static void static_method()
{
System.out.println("Access static variable from static method = "+static_variable);
/*System.out.println("Access non-static variable from static method = "+non_static_variable); can't do this coz of non-static variable.*/
}
public void non_static_method()
{
System.out.println("Access static variable from non-static method = "+static_variable);
System.out.println("Access non-static variable from non-static method = "+non_static_variable);
}
public static void main(String[] arun)
{
new StaticMethodExample().static_method();
new StaticMethodExample().non_static_method();
}
}
Output =>
Access static variable from static method = 99
Access static variable from non-static method = 99
Access non-static variable from non-static method = 98