Class, Methods and Object Creation

Introduction To JAVA

Class, Methods and Object creation

 

Class

What is class?

  1. Class is a collection of state/variables/data and behavior/function/method.
  2. All the statements should be written inside class except Import and package statement. (Class Methods and Object)

 

Methods

  1. Methods are the individual execution block.
  2. Methods are the collection of Variables and executable statements/logical code.
  3. Each method has return data and can have input data.
  4. Return Data is always use to return the output of the method. Can return maximum one value at a time.
  5. Input data is always use to accept any value inside method. Can accept multiple values at a time.
  6. Methods are not executes automatically, we have to call/invoke methods by using object.
  7. Syntax:

 

Object

  1. Objects are the representation of class.
  2. Each Object has independent existence.
  3. By Using object can access/call/invoke class variables and methods.
  4. To call member (variable/method) of class we have to use dot(.) operator.
  5. new operator is use to create class object.
  6. Object creates inside Heap memory of JVM.
  7. Syntax:

 

 

Internals of Object creation and method execution  

  1. JVM use Heap and Stack memory to perform Object creation and method execution operations.
  2. All Objects created inside code will be store inside heap memory, Garbage Collection(GC) process happens on heap memory.
  3. All method executions done inside stack memory, stack memory keep on cleaning once method execution is completed.
  4. All memory created by JVM will be on RAM.

 

Example :-

public class ClassMethod
{
	public static void main(String args[])
	{
		System.out.println("This is main method");
		ClassMethod Obj = new ClassMethod();
		Obj.mul();
		Obj.div(12,6);
		int x = Obj.sub();
		System.out.println("sub : "+ x);
		int y = Obj.add(15,10);
		System.out.println("add : "+ y);

	}

	// Method to print the mul(*) of numbers
	public void mul() {
		int num1 = 30;
		int num2 = 12;
		System.out.println("Multification is : " + (num1 * num2));
	}

	// Method to accept the 2 int number and print div(/)
	public void div(int n1, int n2){
	
		System.out.println("Division is : " + (n1/n2));
	}
	// Method to return the sub(-) of 2 number

	public int sub(){
		int n1 = 30;
		int n2 = 12;
		int ans = n1-n2;
		return ans;
	}
	
	// method to accept 2 int number and return sum of number

	public int add(int n1, int n2)
	{
		int ans = n1+n2;
		return ans;
	}
}

 

 

Comments in Java

  1. Comments is a part of program which will not consider during the execution.
  2. Can write code level documentation by using comments.
  3. In Java comments are of 3 types
    1. Single Line Comment
      1. Is use to comment the single line from the code
      2. syntax:

// single line comment

 

  1. Multi Line Comment
    1. Is use to comment multiple line
    2. Syntax:

/*

Line1

Line2

*/

 

  1. Documentation Comment
    1. Is it use for code level documentation which will adds into the Java documentation.
    2. Syntax:

/**

Comments.

*/

 

Join WhatsApp : Click Here

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

 

Job For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Us !