Classes

A class can be considered as a blueprint using which you can create as many objects as you like. For example, here we have a class Website that has two data members (also known as fields, instance variables and object states). This is just a blueprint, it does not represent any website, however using this we can create Website objects (or instances) that represents the websites. We have created two objects, while creating objects we provided separate properties to the objects using constructor.

public class Website {
//fields (or instance variable)
String webName;
int webAge;

// constructor
Website(String name, int age){
this.webName = name;
this.webAge = age;
}
public static void main(String args[]){
//Creating objects
Website obj1 = new Website(“universitymcqs.com”, 5);
Website obj2 = new Website(“google”, 18);

//Accessing object data through reference
System.out.println(obj1.webName+” “+obj1.webAge);
System.out.println(obj2.webName+” “+obj2.webAge);
}
}

OUTPUT

universitymcqs.com 5

google 18