Class, Methods and Object creation
Class
What is class?
- Class is a collection of state/variables/data and behavior/function/method.
- All the statements should be written inside class except Import and package statement. (Class Methods and Object)
Methods
- Methods are the individual execution block.
- Methods are the collection of Variables and executable statements/logical code.
- Each method has return data and can have input data.
- Return Data is always use to return the output of the method. Can return maximum one value at a time.
- Input data is always use to accept any value inside method. Can accept multiple values at a time.
- Methods are not executes automatically, we have to call/invoke methods by using object.
- Syntax:
Object
- Objects are the representation of class.
- Each Object has independent existence.
- By Using object can access/call/invoke class variables and methods.
- To call member (variable/method) of class we have to use dot(.) operator.
- new operator is use to create class object.
- Object creates inside Heap memory of JVM.
- Syntax:
Internals of Object creation and method execution
- JVM use Heap and Stack memory to perform Object creation and method execution operations.
- All Objects created inside code will be store inside heap memory, Garbage Collection(GC) process happens on heap memory.
- All method executions done inside stack memory, stack memory keep on cleaning once method execution is completed.
- 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
- Comments is a part of program which will not consider during the execution.
- Can write code level documentation by using comments.
- In Java comments are of 3 types
- Single Line Comment
- Is use to comment the single line from the code
- syntax:
- Single Line Comment
// single line comment
- Multi Line Comment
- Is use to comment multiple line
- Syntax:
/*
Line1
Line2
*/
- Documentation Comment
- Is it use for code level documentation which will adds into the Java documentation.
- Syntax:
/**
Comments.
*/
Join WhatsApp : Click Here
Join Telegram : Click Here
All Full Stack Java Study Material
Job For Fresher