Returning a static variable [closed] - java

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.

Related

Is static reference faster than declaration/reference? [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 2 years ago.
Improve this question
I want to run a method at program shutdown which requires a temp variable to an object reference to be stored. I just so happen to have a class variable of the same type that I've been using this whole time to store the current value, and it's no longer needed.
I know it's probably not best practice, but if I want to save the extra step and write an object reference as a placeholder while my program does some housekeeping, would it be faster to write it to this variable which already exists than to declare a fresh temp from scratch? The static variable is in another class, if that makes a difference.
"So fast it doesn't matter," and either way an object gets written. I'm just trying to understand how these things work in memory. The glaring issue with a new variable seems to me that it's a declaration which has to allocate some space on the disk.
Reusing some unrelated class variable is the worst choice. It makes your code unreadable and unmaintainable.
Allocating an additional reference variable on the stack also doesn't take time - it doesn't matter whether your method has 5 or 50 local variables. Allocating local variables means just adding/subtracting some (constant) value from the stack pointer.
Forget about allocating space on the disk - the state of your running program is normally not written to the disk at all.

What are the importance of prefixes in member 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 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)

Which is correct definition of "Abstraction in Java." [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 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.

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

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.

Categories