- Object of a class can be defined at 5 different places.
- Defining object of a class as local to a method or local variable.
- Defining object of a class as an instance variable of another class.
- Defining object of a class as the parameter of the method.
- Defining object of a class as the return data type of the method.
- 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:
- package com.instanceofjava;
- public class ObjectInsideMethod {
- int val;
- public int getVal()
- {
- return val;
- }
- public void setVal(int val) {
- this.val = val;
- }
- public void createObject(){
- ObjectInsideMethod obj=new ObjectInsideMethod();
- obj.setVal(10);
- System.out.println(obj.getVal());
- }
- public static void main(String[] args) {
- ObjectInsideMethod obj=new ObjectInsideMethod();
- obj.createObject();
- }
- }
2.Defining object of a class as an instance variable of another class:
- package instanceofjava;
- class sample{
- int num;
- public void setNum(int num) {
- this.num= num;
- }
- public String getNum() {
- return num;
- }
- }
- package com.instanceofjava;
- public class ObjectAsInstanceVariable {
- Sample sample= new Sample ();
- public static void maina(String args[]){
- ObjectAsInstanceVariable obj=new ObjectAsInstanceVariable();
- obj.sample.setNun(10);
- }
- }
3.Defining object of a class as the parameter of the method.
- package com.instanceofjava;
- public class Employee
- {
- String name;
- int id;
- String address;
- Employee(String name, int id, String address){
- this.name=name;
- this.id=id;
- this.address=address;
- }
- public boolean comapreEmpids(Employee e){
- if(this.id==e.id){
- return true;
- }
- return false;
- }
- public static void main(String args[]){
- Employee empone= new Employee("sai", 1, "hyderabad");
- Employee emptwo= new Employee("sai", 2, "Khammam");
- boolean check=empone.comapreEmpids(emptwo);
- System.out.println(check); //returns false
- }
- }
4.Defining object of a class as the return data type of the method.
- package com.instanceofjava;
- public class Singleton
- {
- static Singleton obj;
- private Singleton(){
- }
- public static Singleton getInstance(){
- if(obj!=null){
- return obj;
- }
- else{
- obj=new Singleton();
- return obj;
- }
- }
- public static void main(String[] args) {
- Singleton obj=Singleton.getInstance();
- Singleton obj1=Singleton.getInstance();
- }
- }
5.Defining object of a class as the static variable or class variable of another class
- package com.instanceofjavaforus;
- public class Sample {
- public void diplsyString(String str){
- System.out.println(str);
- }
- }
- package com.instanceofjavaforus;
- public class TestDemo {
- public static Sample sample= new Sample();
- public static void main(String args[]){
- TestDemo.sample.diplsyString("Hello Hyderabd");
- }
- }
This scenario used in System.out.println();
Great piece of information, this example you have provided are worth knowing and gives more knowledge about java programming language.
ReplyDeleteReally 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