Let's say I have this code. When using an instance of ClassA, how do I pass the instance of the class to the method methodA
?
public class ClassA {
public static int methodA(ClassA class) {
return 1;
}
}
//This is wrong but this is what I'm trying to do
public class Main {
public static void main(String [] args) {
ClassA classa = new ClassA();
classa.methodA(classa);
}
}
The way to achieve this is correct. Just use another name, because class
is a reserved keyword for class definition
public static int methodA(ClassA instance) {
return 1;
}
Your approach works but you must not use the keyword class
as a variable name. Try
public class ClassA {
public static int methodA(ClassA clazz) {
return 1;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.