Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance of any class as input of a method in java

public class Class{
   public void method(class a, class b){
      //some stuff
   }
}

What i want is that a and b can be instances of any class. Is this legal in java? Is there any way to do this?

Thanks.

like image 984
nom Avatar asked Dec 02 '22 14:12

nom


2 Answers

Use Object. Any class in Java is a (direct or indirect) sub-class of Object.

public class MyClass {
   public void method(Object a, Object b){
      //some stuff
   }
}

Oh, and don't use the class name "Class". It's already used in Java.

like image 159
Eran Avatar answered Dec 04 '22 02:12

Eran


Your given code is not legal because class is a keyword in java see JLS 3.9. Keywords

USe can use Object class from JLS 4.3.2. The Class Object

The class Object is a superclass (§8.1.4) of all other classes.

Use following:

public class Class{
   public void method(Object a, Object b){
      //some stuff
   }
}
like image 42
Sumit Singh Avatar answered Dec 04 '22 02:12

Sumit Singh