
Let’s discuss about the java class. When you run a java class, the JVM starts looking for the special-written method ( main method ) of the class that looks exactly like:
public static void main( String [] args ){
// some code here
}
OR
public static void main( String… args ){
// some code here
}
Then the JVM executes all the instructions of main method. Every java application has at least one class and one main method. A class can have some member like:
1. Variable:
A variable defines the property of a class. There two types of variable like instance variable and class variable.
2. Constructor:
A class can have one or more constructors. The name of the constructor should be same with the class name. It is basically a special type of method without return type. The constructor can be parameterized or non-parameterized. If you don’t provide any constructor in your class, the JVM will provide the default constructor (non-parameterized). It is used to create the object of a class.
3. Method:
A Method is used to define the behaviour of a class. A class can have one or more methods. The name of the method can be anything. The method can be parameterized or non-parameterized.
4. Nested Class:
A class with in a class is called nested class.
Example of complete class:
package com.test;
/**
* Description of the class
*
* @author KAUSIK JANA
* @version V1.0
*
*/
public class MyJavaClass {
/**
* The variable
*/
public int i;
/**
* The constructor without parameter
*/
public MyJavaClass() {
// Every instruction should be end in a semicolon
System.out.println("I am in default constructor");
}
/**
* The constructor with parameter
*/
public MyJavaClass(String name) {
// TODO Auto-generated constructor stub
System.out.println("I am in parameterized constructor");
}
/**
* This method returns no value.
*/
public void aMethod(){
System.out.println("I am in method");
}
/**
* The main method
* @param args
*/
public static void main(String[] args) {
// Create an object by calling default constructor
MyJavaClass myJavaClass = new MyJavaClass();
// Create an object by calling parameterized constructor
MyJavaClass myJavaClass2 = new MyJavaClass("KAUSIK");
// Calling the method
myJavaClass.aMethod();
}
}






