Translate

Saturday, April 4, 2020

Constructor in Java

Rules of Constructor

  1. Class name and constructor name must be same
  2. Constructor is able to take parameters
  3. Constructor is not allowed returnType
  4. If you are not creating any constructor in your program then J.V.M or compiler create a default constructor.  
Types of Constructor
  1. Default constructor
  2. User define constructor

Default Constructor

Output :
This is instance m1 method.

Note : 
This is default constructor which is created by JVM. 


User define constructor

Output : 
This is user define 0 args constructor
This is user define parameterized constructor
This is instance m1 method
This is instance m1 method

Note :
When object is created constructor called automatically. At time of calling constructor then memory for the object is created in the memory. Here when two object is creating then object calling automatically and by the help of this two object we can call instance method. Here we create two constructor, first one is user define 0 argument constructor and second one is user define parameterized constructor.

Examples 

Example 1 :
Output :
        " Error..."
Note :
Here we are creating one constructor with 0 argument. But question arise from student side that J.V.M create default constructor; but point is that, if we create user define parameterized  constructor then J.V.M not create default constructor so that we have to create user define non parameterized constructor. Hence this code give Error but it gives the importance role of J.V.M. Ultimately we have to create one more default constructor or we can say user define 0 argument constructor to run this program.   


Example 2 :



Output :
Employe Name    : Vinod Kumar Mishra
Employe Id      : 738.374
Employe Salary  : 20000
Employe Name    : Vinod Kumar Mishra
Employe Id      : 738.374

Employe Salary  : 20000
Note
This program can assign the values of instance variable in constructor. But the disadvantage of this program is it give game output on creating new object. To finish this problem see next example.

Example 3 :

Output :
111
Note :
In this program by using 'this' keyword we can assign the value of local variable in instance variable. 'this' keyword shows the instance variable. By the help of 'this' variable we can modify the last example. See Next Example......

Example 4 :

Output :
Employe Name    : Ratan Sharma
Employe ID         : 142.334
Employe Salary   : 18000
Employe Name    : Kunal Sngh
Employe ID         : 143.584
Employe Salary   : 20500

Note :
Here we are using using this keyword to assign the instance value.

Example 5 :

Note :
Constructor can be change value of instance variable but when constructor is completed instance variable get their actual value. That's why it can not be 31 as program designed. 

Example 6 :

Output :
This is Constructor = 2 and 2
This is Constructor = 1
This is Constructor = 0

Note :
In this program we calling constructor in different constructor by using this keyword but this keyword must be come first in constructor scope. If we call after some statement then it will give Error.

Monday, March 30, 2020

Ways to create Object in java, Features, Conventions of java and Compilation Process

Features of Java

  1. Object Oriented 
  2. Simple
  3. Secured
  4. Platform independent 
  5. Robust 
  6. Portal
  7. Architecture Neutral
  8. Dynamic
  9. Interpreted
  10. High Performance
  11. Multi threaded
  12. Distributed
Convention of Java
  1. Class Name should be start with upper latter example Test
  2. Variable should in lower case
  3. Method should be begins with Camel case lower case the upper case 
  4. Package name should be lower letter
  5. Constant should be like :- SIZE 50


Compilation

  1.  Download J.D.K and Install J.D.K in 💻
  2.  Set Path
  3. Write program in NotePad 
  4. Save program with .java extension
  5. Compile program in command prompt Example javac ClassName.java 
  6. Execute program in command prompt Example java ClassName
Ways to create object in JAVA 
  1. New Keyword  
  2. Instance factory method
  3. static factory method
  4. pattern factory method 
  5. new instance method
  6. clone() method.

Sunday, March 29, 2020

Method Concept in Java

Syntax of methods in JAVA
{
  • Syntax    :-  modifierList returnType methodName(parameterList)
  • Example :- public void Addition(int a, int b){.....Logic.....}
}


Types of Methods in JAVA
{
  1.  Static Method :-
  2.  Instance Method.
}


Definition of Methods
{
  • Static     :- Static method in java belongs to class and not its instance is called Static method.  
  • Instance  :- This method are methods which required an object of its class to be created before it can be called. Such methods are called Instance method.

}

Examples of Methods

NOTE : Following example is very important for destroying confusion in methods concept in JAVA.

  • Example 1 : How to call methods 
class Test             
{
      void m1()  // Instance Method
      {
            System.out.println("Instance Method");
            new Test().m2();  // possible :- By objectName                       
            m2();          // possible :- By Directly
            Test.m1();  // possible :- By ClassName
      }
   
      void  m1()  //not possible :- Error
      {

      }

      static void m2()  // Static Method
      {
            System.out.println("Static Method"); 
      }
   
      m3()       //Not possible :- Error
     {
            System.out.println("No returnType"); 
            
     }
     ðŸ‘‡ // Main Method as well
      public static void main(String args[])     
      {
             Test t = new Test();    //Object Creation
           
             t.m1();  // possible :- 
             Test.m1();    // not possible :- Error 
             
             m2();                   //possible :-               
             t.m2();                 //possible :-
             Test.m2();           //possible :- 
      }
      /*
      Output :-
       Instance Method
       Static Method
       Static Method
       Static Method
       Static Method
       Static Method
       Static Method
      */
   
}

Note 1 :- It is possible to call instance method inside the static method by using object only.
Note 2 :- It is possible to call static method inside the static method or instance method by using Class which is recommended by good developers but we can also called directly and by object.
Note 3 :- It is not possible to make more then one method with same name with same parameter.
Note 4 :- Return type of method is mandatory otherwise J.V.M. will give error.

  • Example 2 : Calling Methods with parameters
class Test
{
      👇// instance method with parameters
      void m1(int a, char x)   
      {
            System.out.println(a);
            System.out.println(x);
            void m2()               //Not Possible :- Error 
            {
                   System.out.println("Inner method");
            }
      }
      👇 // static method with parameter
      static void m2(int b, char y)       
      {
            System.out.println(b);
            System.out.println(y);
      }
   
      public static void main(String args[])
      {
            👇// passing parameter in instance method.
            new Test().m1(7, 'K');         
           
            Test.m2(18, 'V');                 
            👆// passing parameter in static method.
      }
      /*
      Output :-  
       7
       K
       18
       V
      */
}
Note 1 :- We should pass values according to method parameters dataType.
Note 2 :- Java not support inner method but java allowed to create inner class.

  • Example 3 : Passing object as a value to method.
class X{}
class Y{}
class Emp{}
class Std{}
class Test
{
      //Here class X works as a Datatype and object x as a variable
      void m1(X x, Emp e)          
      {
            System.out.println("Instance m1 method");
      }
      //Here class Std works as a Datatype & object s as a variable. 
      static void m2(Y y, Std s)   
      {
            System.out.println("Static m2 method");
      }

      public static void main(String args[])
      {
             System.out.println("Main Method");
             Test mainObj = new Test();     
             X xObj = new X();
             Y yObj = new Y();
             Emp eObj = new Emp();
             Std sObj = new Std();
             ðŸ‘‡//passing objects as a parameter.
             mainObj.m1(xObj, eObj);     
           
             Test.m2(yObj, sObj);           
             ðŸ‘†//passing objects as a parameter.
      }
      /*
      Output :-
       Main Method
       Instance m1 method
       Static m2 method
      */
}
Note :- We can also passing object as a value where method parameters dataType is Object.

  • Example 4 : Use of "this" keyword in Method.
class Test
{
     int a = 20, b = 30;
     void m1(int a, int b)  //possible
     { 
            System.out.println(a+b);
            👆//local variable output 444 
            System.out.println(this.a+this.b); 
            ðŸ‘†//Instance variable o/p 50
     }
     static void m2(int a, int b) //static method            
     {
            System.out.println(this.a+this.a); 
            👆 //Not possible :- Error
            Test t = new Test();
            System.out.println(a+b);     
           ðŸ‘† //local variable output 888
            System.out.println(t.a+t.b);     
           ðŸ‘† // Instance variable ouput 50 
     }
     public static void main(String args[])
     {
            Test obj = new Test();
           obj.m1(123, 321);
           obj.m2(345, 543);
     } 
}
Note 1 :- In Java "this" keyword use for accessing instance variable in instance method.
Note 2 :- It give Error when we use "this" keyword inside the static method.

Saturday, March 28, 2020

Variables in java

Variables in JAVA

Types of variable


  1. Local variable
  2. Static variable
  3. Instance variable 

Local Variable 

  • Definition : Variables which are declare inside the constructor, methods and block are called Local variables.       
  • Declaration : Inside class and inside method, constructor and block.
  • Memory Allocation  : Memory allocated when method will start and destroyed when it'll completed.
  • Scope of variable  : Within method, constructor and block. 
  • Store : It store in stack memory. 
  • Example :- 
{
       void m1()
      {
             int b;           // Error (Must assign value)                                  
             int a=10;    // local variable
             System.out.println(a);   // possible
      }

      void m2()
      {
            System.out.println(a);         //not possible
      }
}
Note :- We must assign value for local variables otherwise it will give error. 


Static Variable

  • Definition : Variables which are declare inside the class but outside method, constructor  and block with static Modifier is called Static variables.
  • Declaration : Inside class but outside constructor, method and block with static modifier.
  • Memory Allocation : When .class file is loading memory allocated and when .class file is unloading memory destroyed. 
  • Scope of variable : Within the class.
  • Store : It store in stack memory.
  • Example  :-

class Test
{
      static int a = 20;  //static variable declaration
      static void m1()  //static method
     {
           Test t = new Test(); //object creation
           System.out.println(Test.a);     
           ðŸ‘†//Possible (By class)
           System.out.println(t.a);                   
           ðŸ‘†//Possible(By object)
           System.out.println(a);
           ðŸ‘†//Possible(Direct access)
     }
     void m2()                    //Instance method
     {
           Test t1 = new Test(); //object creation
           System.out.println(Test.a);       
          👆//Possible (By class)    
           System.out.println(t1.a);               
          👆//Possible(By object)       
          System.out.println(a);
          👆//Possible(Direct access)                     
     }
}
Note : In Instance and Static method static variable can be access by direct access, by using object name and by using class name. In static variable J.V.M provide default value. 


Instance Variable 
  • Definition : Variable which are declare inside the class but outside the method, constructor and block.
  • Declaration : Inside the class but outside the constructor, method and block.
  • Memory Allocation : Memory allocated when object is created and destroyed if object is destroyed. 
  • Scope of variable : Within the class.
  • Store : It store inside heap memory. 
  • Example   :- 

class Test
{
      int  a = 200;
      static void m1()
      {
              Test t = new Test();                         
              System.out.println(a);               // not possible
              System.out.println(t.a);             // possible
              System.out.println(Test.a);       // not possible
      }
      void m1()
      {
             Test t1 = new Test();                        
             System.out.println(a);                // possible
             System.out.println(t.a);              // possible
             System.out.println(Test.a);        // not possible
      }
}
Note : In instance method instance variable can be access directly and by using object reference but by class not possible. And in static method it can be access only and only by object reference. In instance variable J.V.M provides default value.

Data Types in JAVA

Data Types

Definition :  It identify the different size and value that can be stored in the variable.


There are two types of Data Types in Java...

  • Primitive
  • Non-Primitive

Primitive :



Non - Primitive : Classes, Interfaces, Arrays and String. 


How to use data type in java


Example : int a = 19;

    Note : "☝This is instance variable..."

  • int   :  data type
  • a     : variable name
  • =     : assignment operator or constant
  • 19   : literal value
  • ;      : statement terminator 




Friday, March 27, 2020

Types of Methods and Classes in Java

Java Contains 22 types of Methods...
{

  1.  instance ()
  2. static()
  3. normal()
  4. abstract()
  5. accessor()
  6. mutator()
  7. overridden ()
  8. overriding()
  9. final()
  10. native()
  11. strictfp()
  12. private()
  13. public()
  14. protected()
  15. default()
  16. instanceFactory()
  17. staticFactory()
  18. synchronized()
  19. nonsynchronized()
  20. inLine()
  21. patter()
  22. callBack()
}




Basic Types of Classes in Java...
{

  1. normal class
  2. abstract class
  3. final class
  4. strictfp class
  5. immutable class
  6. mutable class
  7. public class
  8. default class
  9. singleTon class

}

Java Keyword And 5 main elements of class

Gotovisit51 Keywords of JAVA...

"This keywords are divided into 9 category "

1. DataType  :
  1. byte
  2. short
  3. int 
  4. float
  5. double
  6. long
  7. char
  8. boolean
2. Flow Control Statement
  1. if
  2. else
  3. switch
  4. case
  5. break
  6. while
  7. do
  8. for
  9. continue
  10. default
3. Exception Statement
  1. try
  2. catch
  3. finally
  4. throws
  5. throw
4. Object Level
  1. new
  2. this
  3. super
  4. instanceof
5. Method
  1. void 
  2. return
6. In Java 5
  1. enum
  2. assert
  3. null
7. Unused
  1. goto
  2. const
8. Source File
  1. class
  2. extends
  3. interface
  4. implements
  5. package
  6. import
9. Modifiers 
  1. public 
  2. private
  3. protected
  4. final
  5. static 
  6. abstract
  7. synchronized
  8. strictfp
  9. volatile
  10. transient
  11. native
Total 51....



5 main elements of class

"Class contain Five Elements.."

{
  1. Variable :-visit... 
  2. Methods :-visit...
  3. Construction :-
  4. Instance Block :-
  5. Static Block :-
}

Constructor in Java

Rules of Constructor Class name and constructor name must be same Constructor is able to take parameters Constructor is not allowed re...