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

Related

Getters and Setters positions [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 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.

Naming issue: what name would you give to this interface? [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 7 years ago.
Improve this question
I'm struggling with a naming issue. What name would you give to an interface that have just one method with this signature:
public interface ?
{
boolean isAvailable();
}
Many classes in my application can implement this interface.
Not that it really matters, you can rename it any time afterwards, and with current IDEs, it is really easy to type any name using autocomplete...
That said, if you want it short, use Available, if you want it more self-explanatory, use CanBeAvailable.
Given that the word "available" already ends with "-able", I think it's okay to break with the Java interface naming convention and call it Availability. Another approach, suggested in Programmers, is to use the prefix "Can-", in which case you can call your interface CanBeAvailable.
The below are the standards defined for Naming conventions.
Class - Always be a Noun
Interface - Always be an Adjective
Method - should be a verb
So, think of some adjective which describes the purpose of your interface.

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

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.

Categories