-4

I want to retrieve the reference variable name.

This works fine

    MyClass c1=new MyClass("A0001");
    c1.myMethod(c1);
    System.out.println(c1.Persons.get(0).Name); // output is: A0001_1 

The classes are:

 class MyClass{  
    public List<Person> Persons = new ArrayList<Person>();  
    public String name="";  
    MyClass(String name){  
        this.name=name;  
    }  
    void myMethod(MyClass c){  
        Persons.add(new Person(c));  
    }  
}  

class Person{  
    public static int Num=0;  
    public String Name="";  
    Person(MyClass c){  
        Num++;  
        this.Name=c.name+"_"+Num;  
    }  
}  

but I want it to be like this

    MyClass c1=new MyClass("A0001");
    c1.myMethod();
    System.out.println(c1.Persons.get(0).Name);

class MyClass{  
    public List<Person> Persons = new ArrayList<Person>();  
    public String name="";  
    MyClass(String name){  
        this.name=name;  
    }  
    void myMethod(this){  
         // which Reference Variable calls this method?
        System.out.println("name of the Reference Variable="+???);
        Persons.add(new Person(this));  
    }  

what can I put to ???
how programmatically can we show "c1" in ???

Ahmita
  • 11
  • 1
  • 3
    Hint: you want us to spend our time to help you. So you please spend the tiny bit of time required to make that as easy as possible; starting by using the **preview** functionality to ensure that **all** of your code is properly formatted/indented. Beyond that: it is a really bad idea to have two classes called Person and Person1. What is the second one about? What is the difference between the two classes?! You should also follow naming conventions more closely - field names start lower case; and using `Persons` vs `Person` is obviously were easy to misread. – GhostCat Mar 12 '17 at 13:49
  • 3
    It's not at all clear what you're asking here. And your edit just **undid** the nice formatting that someone had done for you. Again: There's a preview area. Use it. – T.J. Crowder Mar 12 '17 at 13:52

3 Answers3

2

There is no reason to pass same object as parameter to its own method.

Also, there is no reason to make a method to accept another object of the same class as parameter to create Person object.

so if your Person object always creates by MyClass object and needs exact that MyClass object as constructor parameter, do this:

void myMethod(){
    Persons.add(new Person(this));
}
Vadim
  • 4,027
  • 2
  • 10
  • 26
1

I'm not sure to understand your need.

First, your question title makes me thing you want to use the injection/reflexion capabilities of Java. Based on your code and explanations, I doubt you're looking for this complexity.

Second, this part is unclear. How the hell can we guess what you're trying to do here ?

void myMethod(){
    MyClass c=??? // WHAT DO YOU WANT TO DO ?!!!!
    Persons.add(new Person(c));
}

What you want is the constructor of Person1 class to decide of a Class to put the person in ?

If yes, something like this could do the trick (kindof a factory ?) The MyClass keeps track of all create classes.

Besides, i guess that your class should be able to receive Person and Person1.

Code written oustide of IDE, some mistakes might been still here. And once again, this is based on a partial understanding of your post.

class Person1 extends Person {
    Person1() {
        MyClass c= MyClass.getTheClass("My_Awesome_Class");
        this.Name=c.name+"_"+Num;
        c.addPerson1(this);
    }
}

With:

class MyClass {
    // keep track of created classes
    private static HashMap<String,MyClass> classMap = new HashMap<String,MyClass>();

    public List<Person> Persons = new ArrayList<Person>();
    public String name="";

    private MyClass(String name){
        this.name=name;
    }
    void addNewPerson() {
        Persons.add(new Person(c));
    }

    void addPerson1(Person1 p) {

    }

    // lazy factory
    public static MyClass getTheClass(String classname) {
        // class does not exist yet
        if (! classList.containsKey(classname)) {
            //create it
            MyClass theNewClass = new MyClass(classname);
            classList.put(classname, theNewClass);
        }
        return classMap.get(classname);
    }
}

Your code becomes:

// MyClass constructor is no longer directly visible ...
MyClass c1 = MyClass.getTheClass("A0001");
c1.addNewPerson();
System.out.println(c1.Persons.get(0).Name);

Person1 my_pupil = new Person1(); // creates My_Awesome_Class if needed
MyClass c2 = MyClass.getTheClass("My_Awesome_Class");

System.out.println(c2.Persons.get(0).Name);

EDIT:

some answers pointed out that maybe you want the Person classes to have visibity on their Class. Someone answered that part : pass the Class as argument of Person constructor.

EDIT2: well the question has changed since my post ...

LoneWanderer
  • 3,058
  • 1
  • 23
  • 41
1

In Java objects are mostly created on heap memory ,so we don't have name for objects in Java
You can get a hash Code by using this.hashCode();, this is not the address of your object in the heap , we don't get object's address in java
so you can get the name of the class from the object by using this.getClass().getName

Note: when we write MyClass c1=new MyClass("A0001"); , c1 is not the object of class MyClass its a Reference to the instance of class MyClass

To know more about it Check answer in the Following link

Pushkarraj Pujari
  • 666
  • 11
  • 25
  • Thank you very much. Actually I want the Reference name to class MyClass. – Ahmita Mar 12 '17 at 16:31
  • `MyClass c1=new MyClass("A0001");` here `c1` is the reference name of the object created on heap , and if possible please check link given in this answer i have explained with an example on value and reference type – Pushkarraj Pujari Mar 12 '17 at 16:32
  • @Pushkarraj "when we write MyClass c1=new MyClass("A0001"); , c1 is not the object of class MyClass its a Reference to class MyClass" - it is wrong. c1 is a reference to the instance of class MyClass - i.e. object. Please, do not mislead others. – Vadim Mar 12 '17 at 18:09
  • @Vadim yes absolutely c1 is a reference to the instance of class MyClass but c1 is finally an handle to the instance not the object itself , it holds a reference to the object in heap and instance of a Class is different than an Object of a Class . Objects and Instance are not synonyms – Pushkarraj Pujari Mar 12 '17 at 18:37
  • Yes. That's correct. But Java does not have a conception of pointer or reference. So in Java terms c1 is an object. Everything's is a refernce in Java, except primitives. – Vadim Mar 12 '17 at 19:49
  • @Vadim No dear c1 is not an object itself , its true for c++ not for java I am pretty sure about this , c1 is just a 4 byte handle which is referencing to the object created on heap – Pushkarraj Pujari Mar 12 '17 at 20:02