-1

If I do something like this:

class Example {
    int a;
    char b;
    String c;
}

public static void main(String[] args) { 
    Example example = new Example();
    example.a = 5;
    sort (example);
}

//sort is in another class than main
public sort (Object object) {
    //how can I get example.a here?
}

How can I get "a" in method sort, without knowing the definition of class Example?

leonz
  • 1,107
  • 2
  • 10
  • 32
  • Please post real code. It will make your question much clearer. – Hovercraft Full Of Eels Oct 12 '16 at 11:52
  • Just a hint [here](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface). – Mena Oct 12 '16 at 11:53
  • Sorry, but I'm not going to post my code for this question, the code I posted is specific enough to understand what I want. Plus, my real code is quite similar to this, I made an example class. – leonz Oct 12 '16 at 11:54
  • Then what is `array`? It's a variable never declared. This code makes no sense. – Hovercraft Full Of Eels Oct 12 '16 at 11:54
  • Sorry, it's a mistake, fixed it now. – leonz Oct 12 '16 at 11:55
  • how can you sort a single object? – Blip Oct 12 '16 at 11:56
  • This is not the point of the question. I just called the method like that. I could easily make an array of Example, can it be sorted then? – leonz Oct 12 '16 at 11:58
  • 3
    "the code I posted is specific enough to understand what I want" It would appear not to be. – Andy Turner Oct 12 '16 at 11:58
  • If you try to write a method that sorts any objects, can you do it using reflections? `object.getClass().getFields()[0]`? Or if you know the existence of `Example.a` but it is not in the classpath, use `object.getClass().getField("a")`? – SOFe Oct 12 '16 at 12:04
  • You would probably want to edit line 10 to change it to `sort(new Object[]{ example })`? Otherwise I can't understand what you are trying to do, sorting an object with three primitive fields. – SOFe Oct 12 '16 at 12:05

2 Answers2

0

Example class would be inherited by sort class and will implement comparator of type Example. Code would be like:

class sort extends Example implements Comparator<Example>{
    public int compare(Example e1, Example e2){
        if(e1.a > e2.a) return 1;
        else if(e1.a < e2.a) return -1;
        else return 0;
    }   
}

The given code will work for sorting array of Object type Example.

Actually it would be better to implement comparable from Example itself like:

class Example implements Comparable<Example>{
    int a;
    char b;
    String c;
    @Override
    public int compareTo(Example e) {
    if(this.a == e.a)
        return 0;
    else
        return this.a > e.a ? 1 : -1;
    }
}

//For calling sort
public static void main(String args[]){
    ArrayList<Example> arr = new ArrayList<>();
    Collections.sort(arr, new Example());
}

or

public static void main(String args[]){
    Example[] arr = new Example[n];
    Arrays.sort(arr, new Example());
}
Patrick
  • 1,717
  • 7
  • 21
  • 28
Nikhil Pathania
  • 812
  • 6
  • 19
0

You can use getClass() method which is defined in Object class.

Like that:

String s = new String("some String");
System.out.println(s.getClass().getName());

Or for any other instance you created and you want to find out it's type

(If that what your question was, cause it wasn't clear enough)

roeygol
  • 4,908
  • 9
  • 51
  • 88