Static and instance method in java

Static methods are declared using static modifier.Static method can’t access instance method and instance variables directly.In static method we should not use ‘this’ keyword.Static method does not need an instance to be created .It can directly be called as ClassName.Methodname(parameter) To use the instance method we need to create specific instance of the class. public…

Method Overloading and Method Overriding

Method Overloading: Method overloading means there are multiple methods, within a class, with same name but with different parameters.Method return types may or may not be same.Method overloading may happen inside a class or inside a subclass. public class Main {public static void main(String[] args) {Animal a =new Animal();a.Horse();a.Horse(“black”);a.Horse(10);}}class Animal {private String color; public void…

this() and super() call in java

this( ) call : In java ‘this()’ is used to call a constructor from another overloaded constructor in the same class. It should be the first statement within the constructor. public class Car {public static void main(String[] args) {Toyota obj1=new Toyota(6,16);Toyota obj2=new Toyota();System.out.println(obj1.getvalues()); // output 6System.out.println(obj2.getvalues()); //output 0}} class Toyota{private int height;private int width;private int…

Use of ‘this’ and ‘super’ keywords in java

this keyword: Suppose the variable within the class and the parameter passing to the method within the same class are the same. If we try to store a value to the class level variable from within that method then we will use ‘this’ keyword as below. Class  Car {String color;public void blueCar(String color){this.color=color;}public String getColor(){return …

Reference variables and de-referencing.

The following blocks will help to understand the reference variable and de-referencing using java. We have created Ball class with constructor and then created object with this class to understand the reference variables. class Ball {private String color;public Ball(String color) {this.color = color;}} public class Main { public static void main(String[] args)Ball blueBall = new…