Syntax of Public Variable
public type varName = value;
type is any data type in java.
varName is any valid identifier in java. It is a variable name.
value is an optional. You can initialize variable by assigning its value.
Example of Public Variable
Example 1 : Program that illustrates public variable use in java
class Sample
{
public int x;
}
class PublicVariableDemo
{
public static void main(String[] args)
{
Sample sObj = new sObj();
sObj.x = 10;
System.out.println(sObj.x);
}
}
Output
10