what is a static variable in java code example
Example 1: java what is static
In the Java programming language, the keyword static indicates that the particular member belongs to a type itself,
rather than to an instance of that type.
This means that only one instance of that static member is created which is shared across all instances of the class.
Example 2: Static method in java
We can declare a method as static by adding keyword “static” before method name.
Let’s see example on static method in java.
public class StaticMethodExample
{
static void print()
{
System.out.println("in static method.");
}
public static void main(String[] args)
{
StaticMethodExample.print();
}
}
Example 3: static data and static methods in java
class JavaExample{
private static String str = "BeginnersBook";
static class MyNestedClass{
public void disp() {
System.out.println(str);
}
}
public static void main(String args[])
{
JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass();
obj.disp();
}
}