Static Members in java

  • The class level members which have static keyword in their definition are called static members.

Types of Static Members:

  •  Java supports four types of static members
  1. Static Variables
  2. Static Blocks
  3. Static Methods
  4. Main Method (static method)

  • All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
  • Only static variables get memory location, methods will not have separate memory location like variables.
  • Static Methods are just identified and can be accessed directly without object creation.

1.Static Variable:

  • A class level variable which has static keyword in its creation statement is called static variable.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7. }

  • We can not declare local variables as static it leads to compile time error "illegal start of expression".
  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  public static void main(String [] args){
  7.  
  8.    //local variables should not be static
  9.  static int a=10;// compile time error: illegal start of expression

  10. }


  • All static variables are executed by JVM in the order of they defined from top to bottom.
  • JVM provides individual memory location to each static variable in method area only once in a class life time.

Life time and scope:

  •  Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
  • And its scope is class scope means it is accessible throughout the class.

  1. package com.instanceofjava;
  2. class StaticDemo{
  3.  
  4. static int a=10;
  5. static int b=20;
  6.  
  7.  public static void main(String [] args){
  8.  
  9.    System.out.println("a="+a);
  10.    System.out.println("a="+b);
  11.    show();

  12.  }
  13.  
  14. public static void show(){
  15.  
  16.    System.out.println("a="+a);
  17.    System.out.println("a="+b);
  18. }

  19. }

static methods JVM architecture

Duplicate Variables:

  • If multiple variables are created with same name are considered as duplicate variables.
  • In the same scope we can not create multiple variables with same name.
  • If we create it leads to compile time error "variable is already defined".
  • Even if we change data type or modifier or its assigned value we can not create another variable with same name.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4.  
  5. static int a=10;
  6. int a=30;// Compile time error 
  7.  
  8. }

  • But it is possible to create multiple variables with same name in different scopes.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;
  5. int a=30;// Compile time error
  6.  
  7. public static void main(String [] args){
  8.   
  9. // it is allowed to define "a" in this method.
  10. int a=20;
  11.  
  12. }
  13. }

Shadowing:

  • It is possible to create local variables or parameters with same variable name.
  • The concept is called shadowing . It means local variable is a shadow of class level variable.
  • It means when you access it in side method , you will get local variables value but not  from class level.

  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.   

  8.  int a=20;
  9.  System.out.println("a="+a); // prints 20

  10. }
  11. }

 Local preference:

  • When we call a variable compiler and JVM will search for its definition in that method first, if its definition is not found in that method then will search for its definition at class level.
  • If its definition not found at class level also then compiler throws error: can not find symbol.
  • This phenomenon is called local preference.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11. }
  12. }


  • By using class name we can get class level variables value.


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=10;

  5.  
  6. public static void main(String [] args){
  7.  
  8.  System.out.println("a="+a); // prints 10 : method level no definition so access class level

  9.  int a=20;
  10.  System.out.println("a="+a); // prints 20 :method level variable found

  11.  System.out.println("a="+StaticDemo.a); // prints 10 : class level variable
  12.  
  13. }
  14. }


Order of execution of static variables and main method:

  • First all static variables are executed in the order they defined from top to bottom then main method is executed.
Example Program:


  1. package com.instanceofjava;
  2. class StaticDemo
  3. {
  4. static int a=m1();
  5.  
  6. static int m1(){
  7.  
  8. System.out.println("variable a is created");
  9. return 10;
  10.  
  11.  
  12. static int b=m2();
  13.  
  14. static int m2(){
  15.  
  16. System.out.println("variable b is created");
  17. return 20;

  18.  
  19. public static void main(String [] args){
  20.   
  21.  System.out.println("in main method");
  22.  System.out.println("a="+a);
  23.  System.out.println("b="+b);


  24.  
  25. }
  26. }

Output:
  1. variable a is created
  2. variable b is created
  3. in main method
  4. 10
  5. 20

  • we can access static fields using objects but static fields should be accessed in a static way because static fields are class level so even if we change field value using particular object that will change the original value 


  1. package com.instanceofjava;
  2.  
  3. public class StaticDemo {
  4.  
  5.  static int x=10;
  6.  
  7. public static void main(String[] args) {
  8.  
  9.  StaticDemo obj=new StaticDemo();
  10.  
  11. System.out.println(x);
  12.  
  13. obj.x=30;
  14.  
  15. System.out.println(x);
  16.  
  17. StaticDemo obj2=new StaticDemo();
  18.  System.out.println(obj2.x); 
  19.  
  20. System.out.println(StaticDemo.x); // Recommended way to access static variables

  21. }
  22. }

Output:

  1. 10
  2. 30
  3. 30
  4. 30

Final Static Variables:

  • Final static variables are constants.
  • Declaration itself we need to assign value to the final static variables otherwise compiler error will come: "The blank final field  may not have been initialized".
  • And we can not change this value if we try then compiler error will come: The final field  cannot be assigned.


  1. package com.instanceofjava;
  2. class Demo{
  3.  
  4.  final static int MAX_VALUE=10;

  5.  public static void main(String [] args){
  6.  
  7.    System.out.println(MAX_VALUE);

  8. }



Java Variables and Types of Variables


Variable:

  • Variable is a named memory location used to store data temporarily.
  • During program execution we can modify that data

Limitation of Variable:

  •  It can only store single value at a time.
  • It means , always it returns latest modified value.

How can a variable be created?

  • A variable can be created by using data type.
  • As we have two types of data types  we can create two types of variables.
    1.Primitive Variable
    2.Referenced variable
  • The difference between primitive and referenced variable is "primitive variable stores data directly , where referenced variables stores reference/address of object, not direct values"

  1. package com.instanceofjava;
  2.  
  3. class Example
  4. {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9. }


  1. package instanceofjava;
  2. class Test
  3. {
  4.  
  5. public int get(){
  6. return 10:
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11. //primitive variables
  12. int s=50;
  13. int t=get(); 
  14.  
  15. //reference variables
  16. String str="a";
  17. String str1=new String("a");
  18. Example e= new Example();

  19. }
  20. }



What is an object?

  • Technically object means collection of all non static variables and methods memory locations.
  • Object is created using new keyword

Defining a variable:

  • Variable creation with value is called defining a variable
  • <Accessibility modifier><Modifier><Data type><Variable name>=<Value> ;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int x=10;
  6. public static Example e= new Example();

  7. }
  8. }

Declaring a variable:

  • Variable creation without value is called declaring a variable.
  • <Accessibility modifier><Modifier><Data type><Variable name>;


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int a;
  6. public static Example e;

  7. }
  8. }


Initializing/Assigning a variable:

  • Storing a value in a variable at the time of its creation is called initializing a variable.
  • Storing a value in a variable after its creation is called assigning a value to a variable.
  • <Variable_Name>=<Value>;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. //declaring a variable
  6.  int a;
  7.  int x;
  8.  
  9. //assigning a variable
  10.  a=20;
  11.  x=10;
  12.  
  13. // re initialization/ re assignment
  14.  a=30;
  15.  x=20;

  16. }
  17. }


Calling a variable:

  • Reading a value from a variable is called calling a variable.


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5.  int x=10;
  6.   
  7.  //calling x variable for printing
  8. System.out.println(x);
  9.  
  10. // calling x variable for initializing y
  11.  int y=x;


  12. }
  13. }



Types of Variables:

  • Local
  • Static
  • Non static
  • Final
  • Transient
  • Volatile

1.Local Variables:

  • The variables created inside a method or block are called local variables.

Rules:

  • While working with local variables , we must follow below 3 rules.

Rule #1:

  •  Local variables can not be accessed from another method. Because its scope is restricted only within its method.
  • Also we can not guarantee that variable creation , because that method may or may not be called. It leads to compile time Exception : can not find symbol.
 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5. public void show(){
  6.  System.out.println("a:"+a); Compile time error: can not find symbol a.
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11.  int a=10;
  12.  System.out.println("a:"+a);
  13. }
  14. }


Rule #2:

  • Local variable should not be accessed without initialization.

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4. public static void main(String [] args){
  5.  
  6.  int a=10;
  7.  int b;
  8.  System.out.println("a:"+a);
  9.  
  10.  System.out.println("b:"+b);//Error
  11.  // Above statement throws Compile time Error: variable b might not have been initialized
  12.  
  13.  b=20;
  14. System.out.println("b:"+b);
  15.  
  16. }
  17. }


Rule #3:

  • Local variable must be accessed only after its creation statement. because method execution is sequential execution from top to bottom. If we access before  its creation statement it leads compile time Error : Can not find symbol.


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  static void show(){
  6.  System.out.println("a:"+a);// Compile time Error
  7.  int a;
  8.  System.out.println("a:"+a); // Compile time Error
  9.   
  10.  a=10
  11.  
  12.  System.out.println("a:"+a);
  13.  
  14. }
  15. }

2. Static variables :

  • Static variables gets life when class is loaded.
  • It is destroyed either if class is unloaded from JVM or JVM is destroyed.
  • Its scope is throughout the class it means where class is available there static variable is available provided it is public.

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static int a=10;

  5. public static void show(){
  6.  System.out.println("a:"+a);
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11.  System.out.println("a:"+a);
  12.  
  13. }
  14. }

3. Non Static variables:

  • Non static variable gets life when object is created. It is destroyed when object is destroyed.
  • Object is destroyed when it is unreferenced.
  • Its scope is the scope of the object, object is available only if its referenced variable is available

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  int a=10;

  6. public void show(){
  7.   test t= new test();
  8.  System.out.println("a:"+t.a);
  9. }
  10.  
  11. public static void main(String [] args){
  12.  
  13.   test t= new test();
  14.  System.out.println("a:"+t.a);
  15.  
  16. }
  17. }


4.Final variables:

  • The class level or local variable that has final keyword in its definition is called final variable.

Rule:

  •  Once it is initialized by developer its value can not be changed. If we try to change its value it leads to Compile tile error.


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static final int a=10;
  5. static final int b=20;

  6. public static void main(String [] args){
  7.  
  8.  a=20;//Error : variable might be already have been assigned
  9.  b=30; //Error : variable might be already have been assigned
  10.  
  11. }
  12. }


5. Transient variable:

  • The class level variable that has transient keyword in its definition is called transient variable.

Rule:

  • Local variable can not be declared as transient.
  • It leads to Compile time error: Illegal start expression


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static transient int a=10;
  5. static transient  int b=20;

  6. public static void main(String [] args){
  7.  
  8.  transient int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • We declare variable as transient to tell to JVM that we do not want to store variable value in a file in object serialization. Since local variable is not part of object , declaring it as transient is illegal.
  • Refer IOStreams for more details

6.Volatile Variable:

  • The class level variable that has volatile keyword in its definition. 

Rule:

  • Local variable can not be declared as transient.
  • It leads to Compile time error: Illegal start expression


 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static volatile int a=10;
  5. volatile int b=20;

  6. public static void main(String [] args){
  7.  
  8.  volatile int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • We declare variable as volatile to tell to JVM that we do not want to modify variable value concurrently by multiple threads.
  • If we declare variable as volatile multiple threads are allowed to change its value in sequence one after another one.


You might like:

Top 15 Garbage Collection Interview Questions

Top 50 Java Questions everybody should know

Top 25 Programs asked in interviews

Top 25 Core Java Interview Questions

Accessibility modifiers examples


  • The keywords which define accessibility permissions are called accessibility modifiers.
  • Java supports four accessibility modifiers to define accessibility permissions at different levels.

Accessibility modifier keywords: 

 1.private

 2.protected

 3.public

 4.default(no keyword)

1.private:

  • The class members which have private keyword in its creation statement are called private members. Those members are only accessible within that class.
  • If we declare any variable or method with private accessibility modifier then those variables and methods are accessible only within that class , not accessible out side the class.

private variable are accessible within the class :


  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.first_name="James"; // ERROR: The field PrivateDemo.first_name is not visible
  10.  }
  11.  
  12. }

private methods are accessible within the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. private void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. First Name:=James
  2. Last Name:=Goosling

private variable are not  accessible out side the class :

  1. package com.instanceofjava;
  2.  
  3. public class PrivateDemo {
  4.  
  5.     private String first_name;
  6.     private String last_name;
  7.  
  8. void show(){
  9.  
  10.   System.out.println("First Name:="+first_name);
  11.   System.out.println("Last Name:="+last_name);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PrivateDemo obj= new PrivateDemo ();
  18.  
  19.         obj.first_name="James";
  20.         obj.last_name="Goosling";
  21.       
  22.  
  23.     }
  24. }

  1. package instanceofjava;
  2.  
  3. class Demo {
  4.  
  5. public static void main(String[] args){
  6.  
  7. PrivateDemo obj= new PrivateDemo ();
  8.  
  9.         obj.add(); // ERROR: The method add() from the type PrivateDemo is not visible
  10.  }
  11.  
  12. }



  • private variables and methods are accessible inside that class only. If we declare any variable or method as private , not accessible outside the class.

2.protected

  • The class members which have protected keyword in its creation statements are called protected members. Those members can be accessible with in package from all classes, but from out side package only in subclass that too using subclass name or its object.
  • protected variables accessible inside the package anywhere. Outside package accessible only in sub classes.

Same package anywhere:

  1. package com.instanceofjava;
  2.  
  3. public class ProtectedDemo {
  4.  
  5.     protected int a;
  6.     protected int b;
  7.  
  8. protected void show(){
  9.  
  10.   System.out.println("a="+a);
  11.   System.out.println("b="+b);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        ProtectedDemo obj= new ProtectedDemo ();
  18.  
  19.         obj.a=12;
  20.         obj.b=13;
  21.         obj.show();
  22.  
  23.     }
  24. }

Output:

  1. a=12
  2. b=13

Different package subclass:

  1. package com.accesiblitymodifiers;
  2.  
  3. public class Sample extends ProtectedDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.        Sample  obj= new Sample();
  8.  
  9.         obj.a=12;
  10.         obj.b=13;
  11.         obj.show();
  12.  
  13.     }
  14. }

Output:

  1. a=12
  2. b=13

3.public

  • If we declare any variable or method with public access specifier then those members will be accessible to everywhere.


  1. package com.instanceofjava;
  2.  
  3. public class PublicDemo {
  4.  
  5.     public int x;
  6.     public int y;
  7.  
  8. public void show(){
  9.  
  10.   System.out.println("x="+x);
  11.   System.out.println("y="+y);
  12.  
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17.        PublicDemo obj= new PublicDemo ();
  18.  
  19.         obj.x=1;
  20.         obj.y=2;
  21.         obj.show();
  22.  
  23.     }
  24. }


Output:

  1. x=1
  2. y=2

4.default

  • If we declare any member with no keyword those members are called default members.
  • default members are accessible to package level.
  • Means we can access anywhere in same package but we can not access in out side the package under any condition.
  • So default will acts as public inside package and private out side the package.

Java data types with examples


  1. Primitive data types or Fundamental data types.
  2. Referenced data types or Derived data types.



1.Primitive Data types


1.byte

  1. package com.instanceofjava;
  2.  
  3. public class ByteDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         byte a=10; // a is a variable of type byte holding the value 1;
  8.         byte b=20;// b is a variable of type byte holding the value 2;
  9.         
  10.         byte c= (byte)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

2.short

  1. package com.instanceofjava;
  2.  
  3. public class ShortDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         short a=1;
  8.         short b=2;
  9.         
  10.         short c= (short)(a+b);
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 3

3.int

  1. package com.instanceofjava;
  2.  
  3. public class IntDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a=10; // a is a variable of type integer holding the value 10;
  8.         int b=20;// b is a variable of type integer holding the value 20;
  9.         
  10.         int c= a+b;
  11.         System.out.println(c);
  12.  
  13.     }
  14. }

Output:

  1. 30


4.long

  1. package com.instanceofjava;
  2.  
  3. public class LongDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         long a=1234567890;
  8.         long b=1234567890;
  9.  
  10.         long c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135780


5.float

  1. package com.instanceofjava;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         float a=1234567.8f;
  8.         float b=1234567.8f;
  9.  
  10.         float c= a+b;
  11.  
  12.         System.out.println(c);
  13.  
  14.     }
  15. }

Output:

  1. 2469135.5


7.double

  1. package com.instanceofjava;
  2.  
  3. public class DoubleDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         double a=1234567910112181314151634444422233334491.1d;
  8.         double b=1234567910112181314151634444422233334491.8d;
  9.         double c= a+b;
  10.         System.out.println(c);
  11.  
  12.     }
  13. }

Output:

  1. 2.4691358202243627E39



8.char

  1. package com.instanceofjava;
  2.  
  3. public class CharDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         char a='A';
  8.         char b= 'B';
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.       System.out.println(a+b);
  13.  
  14.     }
  15. }

Output:

  1. A
  2. B
  3. 131


9.boolean

  1. package com.instanceofjava;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         boolean a=true;
  8.         boolean b= false;
  9.  
  10.       System.out.println(a);
  11.       System.out.println(b);
  12.   
  13.  
  14.     }
  15. }

Output:

  1. true
  2. false

1.Referenced Data types

1.class


  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.  int a,b; 
  5.  
  6. void add(){
  7. System.out.println(a+b);
  8. }

  9. public static void main(String[] args) {
  10.  
  11.        Sample obj= new Sample();
  12.  
  13.        obj.a=10;
  14.        obj.b=12;

  15.        obj.add();
  16.  
  17.     }
  18. }

Output:

  1. 22

2.Array

  1. package com.instanceofjava;
  2.  
  3. public class ArrayDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.         int a[]={1,2,3,4,5,6};
  8.  
  9.         for (int i = 0; i < a.length; i++) {
  10.             System.out.println(a[i]);
  11.         }
  12.   
  13.  
  14.     }
  15. }

Output:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6


Constructors in java





  • Constructors will be executed when the object is created.
  • Constructors should have same name of class name.
  • Executed once per object.
  • Basically used to assign instance variables
  • We can overload constructors.
  • We can call super class constructor form sub class constructor.






  1. package instanceofjava;
  2. class A{
  3. A(){
  4. }
  5. }
  6. }

  • while creating object constructor will be executed so we can assign instance variables to some default values.

  1. package instanceofjava;
  2. class A{
  3. int a,b
  4. A(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  A a=new A(10,20);

  11. }
  12. }

Types of constructors:

  • There are two types of constructors 
  • Default constructors
  • Parameterized constructor.

Default constructor:

  •  Default constructor will not have any arguments.
  • If we not defined any constructor in our class. JVM automatically defines a default constructor to our class.
  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(){
  5. a=37;
  6. b=46;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample();
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Parameterized constructor


  1. package instanceofjava;
  2. class Sample{
  3. int a,b
  4. Sample(int x, int y){
  5. a=x;
  6. b=y;
  7. }
  8.  
  9. public static void main(String[] args){
  10.  
  11.  Sample obj=new Sample(37,46);
  12.   System.out.println(obj.a);
  13.   System.out.println(obj.b);
  14.  
  15. }
  16. }

Output:

  1. 37
  2. 46

Constructor overloading:

  1. package instanceofjava;
  2. class Sample{
  3. int a,b 
  4. Sample(){
  5. this(1,2);
  6.  System.out.println("Default constructor");
  7.  
  8. }
  9. Sample(int x , int y){
  10.  
  11. this(1,2,3); 
  12. a=x;
  13. b=y;
  14.  System.out.println("Two argument constructor");
  15.  
  16. }
  17.  
  18. Sample(int a , int b,int c){
  19.  System.out.println("Three argument constructor")
  20. }
  21. public static void main(String[] args){
  22.  
  23.  Sample obj=new Sample();
  24.   System.out.println(obj.a);
  25.   System.out.println(obj.b);
  26.  
  27. }
  28. }


Output:

  1. Three argument constructor
  2. Two argument constructor
  3. Default argument constructor
  4. 1
  5. 2





This and Super Keywords


  • this keyword used to store current object reference.
  • super keyword used to store super class reference in subclass object.

this keyword:

  • Predefined instance variable to hold current object reference.

It must used explicitly if non-static variable and local variable name is same.

 

 





  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0
  • Without this keyword if variable names are same we can not assign instance variables so this problem will solve by using this keyword

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2

Used to invoke current class constructor:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

  1. Default constructor called
  2. 1
  3. 2

Used to call current class method:

 

  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.     int a, b;
  5.  

  6.  Sample(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(obj.a);
  22.     System.out.println(obj.b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Sample obj= new Sample(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }



Output:

  1. Show() method called
  2. 1
  3. 2

super keyword:

  • Predefined instance variable used to hold super class object reference through sub class object.

 

Used to call super class constructor:



  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. public static void main(String[] args){
  12.  
  13.         B obj= new B(1,2);
  14.         
  15.     
  16. }
  17.  
  18.  }
  19.  
  20. }

Output:

  1. super class constructor called
  2. 1
  3. 2

Used to call super class methods from subclass:


  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. void add(){
  15. System.out.println("super class method called");
  16. }
  17.  
  18. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. void print(){ 
  12. super.add();
  13. System.out.println(this.a);
  14. System.out.println(this.b);
  15.  


  16. }
  17. public static void main(String[] args){
  18.  
  19.         B obj= new B(1,2);
  20.         
  21.        obj.print();
  22. }
  23.  
  24.  }
  25.  
  26. }

Output:

  1. super class method called
  2. 1
  3. 2

Abstraction

  • Abstraction is fundamental principle of modeling. A system model is created at different levels, starting at the higher levels and adding more levels with more details as more is understood about the system. When complete, the model can be viewed at several levels.
  • So abstraction is about,
  • Looking only at the information that is relevant at that time
  • Hiding details so as not to confuse the bigger picture


  • Abstraction is a process of developing a class by hiding or removing non essential details relevant to user. here non essential details means method body and logic 
  • Basically Abstraction provides a contract between a service provider and clients.
  • we can achieve this by using abstract class

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • Abstract class can not be instantiated directly.
  • Means we can not create object for abstract class directly

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     abstract void add(); // abstract method
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9.  
  10. public static void main(String[] args){
  11.  
  12. AbstractDemo   obj= new AbstractDemo(); 
  13. // ERROR: Cannot instantiate the type AbstractDemo
  14.  
  15. }
  16. }

Can we declare abstract methods as static?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     static abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.

Can we declare abstract methods as final?


  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.


Can we declare abstract methods as private?

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     final abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }
  • No, we are not allowed to declare method as static . It leads to compile time error.
  • Because it should be inherited to sub class.

 Legal modifiers allowed in combination with abstract

 1.native

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     native abstract void add(); // ERROR: illegal combination of modifiers
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 2.protected

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.     protected abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }

 3.public

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.    public abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }



 4.default

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


How it works:

  1. package Abstraction;
  2. public abstract class AbstractDemo {
  3.  
  4.   abstract void add();
  5.  
  6.  void show(){ // normal method
  7.         System.out.println("this is concrete method present in abstract class");
  8.  }
  9. }


  1. package Abstraction;
  2. public abstract class Example extends AbstractDemo {
  3.  
  4.   abstract void add(){
  5.   System.out.println("this is implemented method in sub class");
  6. }
  7.  
  8.  
  9. }
Select Menu