» » » 5 different places to define Object of a class in java

different places to define object of a class


  • Object of a class can be defined at 5 different places.
  1. Defining object of a class as local to a method or local variable.
  2. Defining object of a class as an instance variable of another class.
  3. Defining object of a class as the parameter of the method.
  4. Defining  object of a class as the return data type of the method.
  5. Defining object of a class as the static variable or class variable of another class.

1.Defining object of a class as local to a method or local variable:


  1. package com.instanceofjava;
  2.  
  3.  public class ObjectInsideMethod { 
  4.  
  5.    int val;  
  6.  
  7. public int getVal()
  8.  {     
  9.   
  10.   return val;   
  11.  
  12.   }
  13.  
  14. public void setVal(int val) {  
  15.  
  16.        this.val = val;
  17.  
  18.  }
  19.  
  20. public void createObject(){ 
  21.   
  22.        ObjectInsideMethod obj=new ObjectInsideMethod();        
  23.         obj.setVal(10);    
  24.  
  25.       System.out.println(obj.getVal());
  26.  
  27.   }
  28.  
  29.    public static void main(String[] args) {
  30.  
  31.         ObjectInsideMethod obj=new ObjectInsideMethod();
  32.         obj.createObject();
  33.  
  34.  }
  35. }





2.Defining object of a class as an instance variable of another class:


  1. package instanceofjava;
  2.  
  3. class sample{
  4.  
  5.   int num;
  6.  
  7. public void setNum(int num) {
  8.         this.num= num;
  9. }
  10.  
  11. public String getNum() {
  12.         return num;
  13.     }
  14. }



  1. package com.instanceofjava; 
  2.  
  3. public class ObjectAsInstanceVariable { 
  4.  
  5.  Sample sample= new Sample ();   
  6.  
  7. public static void maina(String args[]){  
  8.  
  9.      ObjectAsInstanceVariable obj=new ObjectAsInstanceVariable();    
  10.      obj.sample.setNun(10);    
  11.  
  12.     }
  13.   }  



3.Defining object of a class as the parameter of the method.


  1. package com.instanceofjava; 
  2.  
  3.  public class Employee 
  4. {  
  5.  
  6.    String name;  
  7.    int id;   
  8.    String address;    
  9.  
  10. Employee(String name, int id, String address){ 
  11.  
  12.         this.name=name; 
  13.         this.id=id; 
  14.         this.address=address; 
  15.  }   
  16.  
  17.  public boolean comapreEmpids(Employee e){
  18.  
  19. if(this.id==e.id){ 
  20.  
  21. return true;  
  22.  
  23. }
  24. return false;
  25. }   
  26.  
  27.  public static void main(String args[]){
  28.  
  29.      Employee empone= new Employee("sai", 1, "hyderabad");
  30.      Employee emptwo= new Employee("sai", 2, "Khammam");
  31.  
  32.      boolean check=empone.comapreEmpids(emptwo);
  33.      System.out.println(check); //returns false
  34.  
  35.     } 
  36. }

 

 4.Defining  object of a class as the return data type of the method.


  1. package com.instanceofjava;  
  2.  
  3. public class Singleton
  4. {

  5. static Singleton obj;
  6.  
  7.  private  Singleton(){
  8.  } 
  9.  
  10. public static Singleton getInstance(){
  11.  
  12. if(obj!=null){ 
  13. return  obj; 
  14. }
  15. else{
  16. obj=new Singleton();
  17. return obj;
  18. }
  19.  
  20. }
  21.  
  22. public static void main(String[] args) {
  23.  
  24.  Singleton obj=Singleton.getInstance();
  25.  Singleton obj1=Singleton.getInstance();
  26.  
  27. }
  28.  }


5.Defining object of a class as the static variable or class variable of another class


  1. package com.instanceofjavaforus; 
  2. public class Sample {
  3.  
  4.      public void diplsyString(String str){
  5.          System.out.println(str);
  6. }
  7.  
  8.  
  9.  
  10. package com.instanceofjavaforus;
  11.  
  12. public class TestDemo {
  13.  
  14.   public static Sample sample= new Sample();
  15.  
  16.   public static void main(String args[]){
  17.  
  18. TestDemo.sample.diplsyString("Hello Hyderabd");
  19.  
  20.  }
  21. }


This scenario used in System.out.println();

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for 5 different places to define Object of a class in java

  1. Great piece of information, this example you have provided are worth knowing and gives more knowledge about java programming language.

    ReplyDelete
  2. Really Great examples. May be i would have known a little in this, but collecting and aggregating is appreciated with very simple steps for each. Very Useful.....

    ReplyDelete

Select Menu