static dataType var = val;
=> Initialize a static variableClass.var
=> Called using Class instead of Objectthis
& super
can't be used in Static method public class ClassName {}
public class Main {}
ClassName.var
=> To access a static variable no need to create a Objectthis
=> It represents current Object ClassName var = new ClassName();
new ClassName();
var.functionName(arguments);
Integer var = val;
Integer var = null;
Double var = val;
var = null
=> De-allocation of Memory in java GC, Pointer will point to nothing, Does not means 0this(arguments);
=> Calls another ConstructorDefault/No Argument
public ClassName () {
var = val;
}
ClassName var = new ClassName();
Parameterized
ClassName (dataType val) {
// "this" keyword is used to access the Property of current Object
this.var = val;
}
ClassName var = new ClassName(val);
Copy
Shallow copy => Any changes made in referenced objects in object X or Y will be reflected in other objects
Deep Copy => A deep copy copies all fields and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers
Creating a Copy Constructor
ClassName (ClassName var) {
this.var1 = var.var1;
}
Calling it
ClassName var = new ClassName(objectName);
Object Cloning => Can be used instead of Copy Constructor
class ClassName implements Cloneable {
int var;
ClassName(int var) {
this.var = var;
}
// For shallow copy
public Object clone() throws CloneNotSupportedException {
// To obtain the cloned object reference
return super.clone();
}
public static void main(String args[]) {
try {
ClassName c1 = new ClassName(val);
ClassName c2 = (ClassName)c1.clone();
} catch(CloneNotSupportedException c) {}
}
}
Destructor
finalize()
extends
=> This keyword is used public class ClassName1 extends ClassName {
// Statements
}
super
=> Used to access methods of Parent/Base class from children class public class ClassName1 extends ClassName {
// Statements
}
public class ClassName2 extends ClassName1 {
// Statements
}
public class ClassName1 extends ClassName {
// Statements
}
public class ClassName2 extends ClassName {
// Statements
}
ChildClass c = new ChildClass();
ParentClass c = new ChildClass();
ClassName var = new ClassName1();
=> Making Object from children Class, At compile time it acts as an Object of parent Class and at run time it acts as an Object of child ClassClassName.main(arguments);
=> To call a overloaded main method from default main method@Override
=> Mentioned before writing Overridden method, Optional to write public returnType getVarName () {
return var;
}
public void setVarName (dataType val) {
this.var = val;
}
dataType var = val;
private dataType var = val;
public dataType var = val;
protected dataType var = val;
abstract
keyword is used abstract class ClassName {}