Reputation: 27
I have a question what is the most correct way for making variables that are accesible in all classes??
I know to ways, one harder than the other.
First one:
public class x {
public String z:
}
And second one is making the variables in one class, like locals and with setters and getters make something like this (I dont know to well this one). And in the class you want to use it you only use the constructor of the class
public class x {
public x{
String a:
}
set a (String){
this.a = a
}
}
Or something like that. (I hope you understand the way i Reffer.)
I want to know what is the most correct, in terms of coding quality, and what do professional programmers use.
Upvotes: 0
Views: 51
Reputation: 285403
I want to know what is the most correct, in terms of coding quality, and what do professional programmers use.
In general, the best way is not to do this. When you expose variables you allow anything and everything to be able to change them, and how or when they do so is now out of your control. Not only that, it actually can increase your code's complexity (its "cyclomatic complexity" to be exact), making it ultimately harder to debug or enhance. Better to encapsulate your variables in classes, and then if you absolutely need outside forces to be able to change them, have the object control this as much as possible. Read up on Encapsulation and Java for more on this.
Upvotes: 4