HOW TO CREATE OBJECT FOR A CLASS

In Java, we can create object for a class by using new operator.

Syntax:-
             classname objectname = new classname();
                                        (or)
             classname objectname; //null
             objectname = new classname();

CONSTRUCTOR


1) Constructor name is same as the class name.
2) The code written in the constructor is executed only once.
3) Constructor does not have any return type.
4) The object points variables and methods present in that class.

INTERVIEW TIPS:-

1) For a class we can create any number of objects.
2) For a program the number of .class files generated is equal to the number of classes present in the program.

PROGRAM:- How to create object for a class

class Emp
{
    int eno=10;
    String ename="kumar",desig="manager";
}
class Empobj
{
   public static void main(String args[])
  {
     Emp e=new Emp();

    System.out.println(e);
    System.out.println(e.eno);
    System.out.println(e.ename);
    System.out.println(e.desig);

    Emp e1=e;
    e1=new Emp();

    System.out.println(e1);
    System.out.println(e1.eno);
    System.out.println(e1.ename);
    System.out.println(e1.desig);
  }
}

File Name: - Empobj.java

Commands:-
                    javac Empobj.java
                    java Empobj
OUTPUT:-
                Emp@1e5e2c3->UNSIGNED HEXA DECIMAL CODE 
                10
                kumar
                manager
                Emp@18a992f
                10
                Kumar 
                manager

NOTE:-
  • The UNSIGNED HEXA DECIMAL CODE can vary for your output.
  • By using this UNSIGNED HEXA DECIMAL CODE we can identify whether two objects are same or not.
  • If the UNSIGNED HEXA DECIMAL CODE is same for two objects, then in that case there will be only object and other is reference.
  • If the UNSIGNED HEXA DECIMAL CODE is different for two objects, then these two objects are two different objects present in JVM heap.
PROGRAM:- Passing values to method

class Emp
{
   int eno;
   String ename,desig;

   void set(int no,String name,String des)
  {
    eno=no;
    ename=name;
    desig=des;
  }

  void get()
 { 
     System.out.println("Emp Number:"+eno);
     System.out.println("Emp Name:"+ename);
     System.out.println("Emp Desig:"+desig);
  }
}
class Empdemo
{
   public static void main(String args[])
  {
     Emp e = new Emp();
     e.set(20,"kiran","manager");
     e.get();
   }
}

File Name: - Empdemo.java

Commands:-
                      javac Empdemo.java
                      java Empdemo

OUTPUT:-
                      Emp Number: 20
                      Emp Name: kiran
                      Emp Desig: manager

PROGRAM:- Passing values to constructor

class Emp
{
   int eno;
   String ename,desig;

   Emp(int eno,String ename,String desig)
  {
     this.eno=eno;
     this.ename=ename;
     this.desig=desig;
  }

  void show()
  {
     System.out.println(eno+" "+ename+" "+desig);
   }
}
class Empcon
{
   public static void main(String args[])
  {
     Emp e = new Emp(30,"hari","manager");
     e.show();
   }
}

File Name: - Empcon.java

Commands:-
                     javac Empcon.java
                     java Empcon
OUTPUT:-
                     30 hari manager
NOTE:-
  • If we use same variable name for instance variables and formal parameters then we have to use this keyword to assign the value present in formal parameter to instance variable.
Syntax:- 
             this.instancevariable = formal parameter;