In java can an instance variable and a method have the same name without any instability or conflict?
I want to make sure if I can get away with compiling it, that it wont cause any error down the road.
Yes it's fine, mainly because, syntactically , they're used differently.
There's no problem with giving parameter names and instance variable names the same name. But Java has to pick whether it is an instance variable or a parameter variable. Either way, it doesn't do what you think it should do. It doesn't initialize the instance variables with the values of the parameter variables.
Yes you can, but local variable will hide the class variable.
Java static code analysis: Methods and field names should not be the same or differ only by capitalization.
Yes it's fine, mainly because, syntactically , they're used differently.
It's completely fine because methods and variables are called differently.
Code:
String name = "myVariable";
public String name() {
return "myMethod";
}
System.out.println(name()); // Brackets for method call
System.out.println(name); // No brackets for variable call
Output:
myMethod
myVariable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With