Java Classes and Objects - The Coding Shala
Home >> Learn Java >> Java Classes and Objects
Java Classes
Java classes are a structure or blueprint for which we create objects. When we create a class then it defines a new data type. This new data type is used to create objects of that type.
The class keyword is used to declare a class. A class contains variables and methods. The basic structure of a class is as follows:
class class_name{ type instance_variable1; type instance_variable2; ... .. type instance_variableN; type method_name(parameters){ //body of method } type method_name2(parameters){ //body of method } ... ... type method_nameN(parameters){ //body of method } }
The Variables, defined within a class are called instance variables. The methods and variables defined within a class are called members of the class.
Note: Java classes do not need to have a main() method. If the class is the starting point of our program then the main() method is required.
Declaring Objects
Every time we create a class, we create a new data type. Using this datatype we can create objects. Creating objects of a class is a two-step process. First, Declaration and Second are Initialization. The new operator is used to initialize the objects. It allocates memory for an object and returns references to it. The simple syntax to create an object is as follows:
Class_Type object_name = new Class_Type(); or Class_Type object_name; //object contains null object_name = new Class_Type(); //return referernce of memory to the object
Here, Class_Type is simply named of the class that which type we are creating objects and object_name is the identifier of the object.
Creating Methods
In Java, every class contains methods. The basic form of a method is as follows:
type method_name(parameters){ //body of method return type_value; }
Here, the type specifies the type of data that returned by the method and return statement is used to return that value. If our method does not return a value then its type must be void and no need to write a return statement. We can also pass the parameters that are used to receive the value of the arguments passed to the method while calling it.
The following Java program explains the class and its member(instance variable and method) and how to create an object:
class Student{ //This is a class named Student String name; //instance variable int id; //instance variable int age; //instance variable int subject1Marks; //instance variable int subject2Marks; //instance variable void StudentDetails() { //void method with no parameters System.out.println("Name of Student is: " + name); System.out.println("Id of Student is: " + id); System.out.println("Age of Student is: " + age); System.out.println("Subject1Marks of Student is: " + subject1Marks); System.out.println("Subject2Marks of Student is: " + subject2Marks); } int TotalMarks() { //method that returns int value return subject1Marks + subject2Marks; } int MarksChanged(int value1, int value2) { //method with parameter and return int value this.subject1Marks = value1; this.subject2Marks = value2; //will see this in later chapters return subject1Marks + subject2Marks; } public static void main(String[] args) { //main method Student student1 = new Student(); //creating object student1 student1.name = "Akshay"; student1.id = 1; student1.age = 23; student1.subject1Marks = 85; student1.subject2Marks = 90; student1.StudentDetails(); System.out.println("Total marks of Student is: " + student1.TotalMarks()); System.out.println("Total Marks after change is: "+ student1.MarksChanged(90, 95)); } } <<<<<Output of this program is >>>>>>> Name of Student is: Akshay Id of Student is: 1 Age of Student is: 23 Subject1Marks of Student is: 85 Subject2Marks of Student is: 90 Total marks of Student is: 175 Total Marks after change is: 185
In Java, the Forward declaration is not necessary.
Other Posts You May Like
Comments
Post a Comment