What is the sense of static variables in Java? [closed] - java

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.

Related

In java, why do some developers use the underscore BEFORE for variable names? - Already seeing differences in peoples answers on this post [closed]

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'm reading Martin Fowler's Refactoring book. In many of his refactoring examples, he is using variables that start with _varname. Why? Is this some old convention that is before my time? In the past year when I started learning Java, I have not seen anyone at work use this. Please advise as to where and why should this be used?
I'm already seeing differences in answers to this question...
Why does martin fowler do this in this code for extract method refactoring?
FROM:
void printOwing(double amount) {
printBanner();
//print details
System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}
TO:
void printOwing(double amount) {
printBanner();
printDetails(amount);
}
It is a convention to start the names of private fields of an object with an underscore in order to distinguish them from local variables in the code. This convention is not universal. Personally, I think that it is a bit superfluous when you have syntax highlighting that also shows the difference.
An alternative (although you could also use both) is to always reference members through this:
package org.foo.bar;
class Baz {
private String quux;
Baz (String quux) {
this.quux = quux;
}
String getQuux () {
return this.quux;
}
}
The convention is often used for private fields.
You don't have to use it, the most important is to be consistent, so if you are working on an existing code base, continue with that style.
Most likely, to distinguish local variables from class members.
EDIT: Now that you've added sample code, that is indeed the case.

Programming with public variables [closed]

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).

Returning a static variable [closed]

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.

Why does java use void? [closed]

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 am well aware that the void keyword is used to indicate in a method declaration that the method will return no value. My question is not about - to use an analogy - how to use the car, but rather about what's underneath the hood. I would like to know why void actually needs to be specified, i.e. does Java reserve an internal variable of some sort to keep track of return types? Will setting a method to void make it so that it doesn't have to do this?
Another way to put it is to ask why, exactly, can't Java omit the void keyword and assume that if there is no returned type than the method is void? I think that there is something to do with how Java might "prepare" to handle the return type, and possibly something to do with optimization... please use full detail.
Its a java language Specification
void used with only methods declaration
if we omit void then its treated as constructor.
SEE THIS
my answer is a bit of a guess. But I'd say its to protect the developer against just forgetting to declare the return type. Sure the compiler can even by default be programmed to render undeclared return types void. But that would be at cost to the aid for the developer.
Furthermore, IDEs would have to anticipate on the same.

Is it acceptable if actual and formal parameters have the same name? [closed]

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
like this:
myMethod(first, second)
public void myMethod(int first, int second)
My teacher said it's preferred not to name them the same. But I don't see the reason why? Is he wrong?
The variables should describe what they both are in their own contexts.
For example in the methods context (formal parameters) it might be compared1 and compared2. But in the calling context (actual parameters) it might be myPyjamas and myAuntsPyjamas. If they "mean" the same thing in both contexts then so be it.
So in this example they may be different:
Fanciness comparativeFanciness= comparePyjamas(myPyjamas,myAuntsPyjamas);
public static Fanciness comparePyjamas(Pyjamas compared1,Pyjamas compared2){
.....
}
for anyone wondering my pyjamas are very fancy
But in annother example they may be the same, because they both mean the same thing in both contexts:
setThrustDirection(thrustDirection);
public void setThrustDirection(Vector3d thrustDirection){
.....
}
caveat
It's often a good idea to avoid having a variable name the same as a field name (a.k.a. instance variable name) except in the constructor as you can end up shadowing the field (meaning you have to access the field as this.name and the variable as name)
Depends on the context. If there is a function to reverse a string
def reverseString(theString)
theString is kind of ok for that function but unlikely to be a good variable name in the context of calling that function. If it's a person's name then something like when it is being called
reverseString(firstName)
I think it's a possible shadowing problem. If the class has an instance variable with the same name as the parameter in the method definition, the coder needs to be careful and use this. when referring to the class level variable. I've seen lots of students forget that and wonder why the code doesn't work. I prefer unique names to prevent the problem.
The entire point of naming is to make the code more comprehensible, if your names do then they are more than acceptable, if they don't, maybe you was taught wrong...

Categories