The following program demonstrates the structure of a Java program. A basic Java program consists of a class that has member fields and methods.
class Fundamental
{
public int x; //this is a member field of the class
public Fundamental() //this is the default constructor
{
x = 10;
}
public void printX() //this is a member method of the class
{
System.out.print("x = "+x);
}
public static void main(String []args)
{
Fundamental obj = new Fundamental(); //initializing an object of the class
obj.x = 20;
obj.printX();
}
}
class Fundamental
{
public int x; //this is a member field of the class
public Fundamental() //this is the default constructor
{
x = 10;
}
public void printX() //this is a member method of the class
{
System.out.print("x = "+x);
}
public static void main(String []args)
{
Fundamental obj = new Fundamental(); //initializing an object of the class
obj.x = 20;
obj.printX();
}
}
No comments:
Post a Comment