Introduction To Java Classes

By Kunal Kapoor
19-04-2018
739

Classes are the core of java programming. They contain all the methods and syntax of the code for execution.  It forms the basis and structure for the object oriented programming of java language.

 

A class can be declared by the following. eg:


1. myClass.java

public class myClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		
		// creating an object of nextClass
		nextClass nextclass=new nextClass();	
		nextclass.display_now();
		
		
		// creating a constructor of firstConstructor class
		firstConstructor first=new firstConstructor(200);
		//getting the value of variable i from the constructor element
		System.out.println(first.i);

	}

}
2. firstConstructor.java
public class firstConstructor {
	
	public int i;
	
	public firstConstructor(int input) {
		this.i=input;
	}

}

3. nextClass.java:
public class nextClass {

	public void display_now() {
		System.out.println("Ourcoaching.com");
	}
}

output :
Ourcoaching.com
200


You can create an object of their class by referring to code above

An object is a variable that contains instance of that particular class. This is used to interface all the methods inside that class

 

Contructors

A constructor is an instance object of a class that initializes the values for a particular class.

 

"this" variable

this is particularly used to refer the current object. In many cases when in a class you want to refer to current object multiple times, instead of using the object variable, you can use this to refer to current object.



Related Tutorials