Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I'm pretty new to Android/Java programming and I've seen a lot of tutorials and answers and I've seen that almost all variables are public. So I'm curios is this a bad thing or not to program with public variables, because I've heard that is better to make private variables.
Short explanation to give you an overview:
In classes you talk about private and public variables. Most the time you declare a variable as private and access it with public getter and setter methods. In a procedure you can declare variables gloable or local, but not with a keyword like public/private. The variables are global when you can access them in the whole procedure and they are local when they are for example only in a while loop or and if statement. I hope you have a little overview now.
Take a look at protected variables too.
It is not true that almost all variables are global.
To create global variable in Android you should to create a class that extends the Application class, add them to Android Manifest. After that you can access it from any context using the Context.getApplicationContext() method.
Here is a link which describe how to create global variables in android (link).
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
It is alright to use constructors,getters,functions,methods and setters after the main function,to me this way is more confortable?
You should create another class and put there main method. Class representing anime should be in different class. Also class should start with upper case.
There is no "hard rule" for how methods are ordered, but generally the variables are declared first, then constructors, then object methods with the getters and setters at the bottom. This is part convention and part personal preference as I've seen it done many ways. You should see java conventions: https://www.oracle.com/technetwork/java/codeconventions-135099.html and generally try to stick to those. You have quite a few naming convention Faux pas in your example... class name should be capitalized, method names should be camelCase, new lines between methods, etc.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
What is the sense of making or not making a variable static? Why can't non-static variables be accessed from a static method?
Since string is always the same, should I make it static
YES. To be specific make it like this:
public class A implements ActionListener {
public static final String MY_STRING = "This is a string"; // can be private also
#Override
public void actionPerformed(ActionEvent e){
System.out.println(MY_STRING);
}
}
Thanks for the feedback and I see a good point of Tom's comment :
Extracting everything is not a programming convention and doing
something must make sense and not just "cause no harm -- Tom
I would like to clarify that this answer is only intended to OP's situation. However it can be no-good as what Andy mentioned earlier : making the code harder to grok. If there is no "semantic" meaning to the value. In conclusion this answer might not be suitable in most similar case.
If it's static, it won't be garbage collected. Moreover, if it's a local variable, it shouldn't be accessible to other methods.
The JVM garbage collector can cache (or deduplicate) Strings, so you shouldn't worry too much about this.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Okay, so I am going through the Android development tutorial book: The Big Nerd ranch and I am on chapter two where it tells you that you need to have certain prefixes and not have certain prefixes before getters and setters.
"What is the point of setting these prefixes? Now when you ask Eclipse
to generate a getter for mQuestion, it will create getQuestion()
rather than getMQuestion() and isTrueQuestion() rather than
isMTrueQuestion()."
Why would having getMQuestion() make a difference, wouldn't it be the same as getQuestion()?
Thanks
You put "m" prefix before instance property and "s" prefix before class property and no prefix for local variables. It's a standard so it's easier to distinguish variables scopes when reading class implementation details.
When You are reading code of an object method where you see three variables: mValue, sValue, value
You know that:
- mValue is object property so by modifying it You change object state.
- sValue is class property so if You would change it it will affect all objects of that class
- value is local property which will be garbage
collected as soon as method returns.
this naming convention is class implementation detail which should not be visible from outside as public interface (getters, setters)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am having trouble in understanding the meaning of "Abstraction in Java". I googled, and studied books, in those I got two types of definitions.
Choosing necessary properties and hiding unwanted details is Abstraction.
Abstraction is the concept of simplifying one idea to a more general, overhead idea.
I feel above two definitions don't mean same, and are entirely different.
So which is correct one?
I think both your statement have same meaning if you think deeply.
Hiding necessary properties and hiding unwanted details leads you to more general, overhead idea.
suppose Animal is a abstract class we hide the nature of animal and their food habits in abstract class and when we talk about Tiger we introduce all the revelant details,
Abstraction is hiding of data.
Means IF you have a class A which contain 2 variables suppose int id,String name
in which if you want to keep that data protected in your class you will keep
Id as private variable so your variable is not directly accessible outside class.
This way you can approach to handle abstraction in class.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am programming something in Java and in my main-class, i have a static class variable called "file-system".
Now i want to give the variable file-system to a method from another class, which needs some information from file-system, and modifies it in some ways.
Now I am wondering: Do i have to return the new file-system from my method that is modifying it? Or is the modification taken over to my file-system in main-class due to the "static" attribute?
I don't know what else to write here, but i cannot post my question yet because editor tells me it does not serve the quality standards. Seriously, who had the idea to do a quality analyzing tool which is completely messed up and does not even let me post a single question?
If the static variable is also declared public then your other class can modify it "in place" - just reference the variable:
MyClass.fileSystem = ....
It depends on the type of your static variable. Intrinsic types (such as int, char, boolean) are copied, no matter how static it is, while for objects the reference (or a pointer if it's more clear to you) is kept.