• Post Reply Bookmark Topic Watch Topic
  • New Topic

Editing Variables  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still enhancing my Car Wash project for my Computer Science class, and I've learned so much from doing so - from what an object is and how to use it, to modular programming - but there's one thing that's really bugging me: I can't quite figure out how to use getters and setters. The whole thing just looks like Greek or something when I try to read it from my book, I'd just like someone to dissect the syntax for me. But please, keep it simple (don't go over-the-top with phrases and terms that only seasoned cyber-vets would know).

Main questions: How do I edit the values of variables from other classes? How do I save one variable's value into another?

Here's an example of the situation I'm in:


(Note: I realize that I don't need to call a variable prior to changing the information, it is just an example.)

Also, my CS teacher told me that there were multiple ways to write 'String args' in the main, and I'm just wondering if there's a best way.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


(), brackets are used to call a method. Therefore call.firston() is wrong as there are no method named firston() in class aVars..
You can refer a variable as call.fiston;

Edit a variable is a simple case. If you want to put secondon value into firston value, just write,,,



I think you have no idea about simple concepts in java. Please refer some java fundamental books before reaching OOP..
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find an old (pre‑2003) Java book, you may find different versions of the main method header. In some cases they write [] late, which is poor style. The Java Language Specification gives two versions. In some instances, where the args parameter is used in an anonymous class, the keyword final must precede the type Str…
I recommend you stick to one of the two versions in that JLS link, with or without final.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:Main questions: How do I edit the values of variables from other classes? How do I save one variable's value into another?

Well, as your teacher told you, you use getters and setters.

But before you start doing so, you need to get into the habit of using conventions.

Your class names are still incorrect: As I told you in your other thread, they should start with a CAPITAL letter; method and field names start with lowercase letters (you may see the horrible Pythonesque '_' occasionally. Don't use it). And both should use CamelCase.

Once you've started doing that, writing getters and setters is very simple:

For a field:
private type myField;     (and it should ALWAYS be private - at least until you know a lot more)
its getter signature is:
public final type getMyField() { ...
and its setter signature is:
public final void setMyField(type newValue) { ...

The final is optional, but I always put it in - at least to begin with.

HIH

Winston
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is CamelCase a required or suggested rule? Whichever the case, I plan to start using it as you say - the reasons I write all my classes as lowercaseUppercase is because that's how my teacher does it, and it appeals to me. (My teacher actually told one of the students in my class that writing their class name as "Carwash" was incorrect, as it should be written "carWash" - "That's how it's supposed to be written", she said. [Although she does teach 8 different classes (4 different classes each day), meaning a lot of paperwork, so it may have just been a slight mishap.])

Over on the setters/getters, and regarding Ramesh's response: I feel that I am in need of learning how to use them because the way you mentioned (which is the way I have been using ever since I learned how to use objects) does not work - there are no errors, but at the same time there is no effect on the variables themselves (the variable that is supposed to have its value changed remains the same, and when I try to do vice-verse it grants the same product - which is nothing basically). It's not that I don't get have no grasp on these simple concepts (I kind of find that accusation offensive) it's just that I don't know the syntax (that, and/or my teacher probably hasn't mentioned the actual concept rule of parentheses being used to call methods alone, such as when creating an object for a class (the class name receives parentheses afterwards)). [Apologies: Did you mean that when trying create an object to call a variable from a different class I should not use () at the end of the class name? (I don't mean to seem rude, I'm very tired from school.)]


Winston and Gutkowski wrote:... final ...

I have one question: doesn't final mean that the values of all the information stored within that variable/method can not be changed (hence final)?

Winston Gutkowski wrote:... For a field:
private type myField;     (and it should ALWAYS be private - at least until you know a lot more)
its getter signature is:
public final type getMyField() { ...
and its setter signature is:
public final void setMyField(type newValue) { ...

That's how my book shows it, so thank you for verifying. I thought either the book was wrong or it was holding back some information regarding syntax. Now, based off the syntax, it wouldn't be too outrageous to say that the syntax of a setter/getter is very similar to that of a method, am I right? If which is the case, then that would mean that a person could set multiple variables' values through use of brackets - how would (or rather, how can) this occur? You've answered my question exactly like I requested, now I just need to bother you with a request for an example in how a setter could quite possibly be used to set multiple vari Now I can check my book again for examples on how far setters can go.

Thank you guys for your helpful responses!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:Is CamelCase a required or suggested rule?


Suggested in terms of the language. Java doesn't care. It will work just fine regardless.

Recommended by the naming conventions of Oracle's Java Code Conventions.

Required by the standards of any professional development shop I've ever heard of.

Over on the setters/getters, and regarding Ramesh's response: I feel that I am in need of learning how to use them because the way you mentioned (which is the way I have been using ever since I learned how to use objects) does not work - there are no errors, but at the same time there is no effect on the variables themselves (the variable that is supposed to have its value changed remains the same, and when I try to do vice-verse it grants the same product - which is nothing basically


Create a tiny little class that has a single member variable. Define a getter and setter for that variable. Write a main() that:
1. Creates an instance of that class.
2. Uses the getter to get the value of the member variable.
3. Prints out that value.
4. Uses the setter to set the value of the member variable.
5. Uses the getter to get the value again.
6. Prints the value again.

There should be nothing else in that program.

If that program behaves correctly, then you understand the basics of getters and setters. If not, post your code, exactly what behavior you expected, and exactly what behavior you saw instead.

Moving past that: thank you both (Ritchie and Gutkowski), for your responses. I have one question though: doesn't final mean that the values of all the information stored within that variable/method can not be changed (hence final)?


Yes. On a variable, final means that once it's initialized, the variable's value cannot be changed.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I just tried taking off the parentheses from my object creation...

and it shows that is wrong.

Which even brings me even closer to the conclusion that using getters and setters is the way to go.

[Edit: I'll do as you say and try creating a small code to test out my knowledge of getters/setters, thank you for the suggestion.]
[Edit 2: Final on a variable makes it unable to be edited, but what about when used on a method?]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:So I just tried taking off the parentheses from my object creation...

and it shows that is wrong.


Yes, you need to use parens along with the class name when using the new operator. That's true.

However...

Which even brings me even closer to the conclusion that using getters and setters is the way to go.


...using parens when creating a new instance has nothing whatsoever to even remotely in any way at all do with using getters and setters. I don't even have any idea how you could make the leap from one to the other.

The new operator creates a new instance of a class--a new object. So, for instance, if you're reading a list of student information from a file, then for each line you read (one student's info), you might create a new Student object to hold the data for that student.

Completely and utterly independent of that is getters and setters. They're just methods, like any other, although by convention they retrieve or store state in an object. The only relation between them and the above is that to invoke a getter or setter, you have to have at some point created an object in which to store the data you're getting or setting.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:
[Edit 2: Final on a variable makes it unable to be edited, but what about when used on a method?]


It means it can't be overridden.

In the future, for questions like "What is X?" or "What does X mean?" please SearchFirst(←click). Thanks!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:So I just tried taking off the parentheses from my object creation...
. . .
That has nothing to do with setters; the () represent invoking the constructor.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bro,

From what I can see from your code is that you know things here and there, but it's not organised. I'd say you need to get a hold of programming concepts, like calling a method, or the different ways of writing the String parameter in the main method. Try the Head first Java Programming Book, that is a great place to start.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion I've caused, the leap from object creation to getters and setters wasn't done with a connection (hence leap, I guess) - all I was saying was that editing variables by use of objects wasn't working, so I'll need to use getters and setters. I've actually found out why I can't use objects to edit variables in my program: instead of modularizing my code by simply splitting it up into methods, I split it up into different classes (oops). I went a little too far with that I guess, but I still want to learn how to use getters and setters nonetheless. I'm not sure if this is even a problem in most cases or if it's just JCreator being all funky, but I'll stop using JCreator until I get a better grasp on Java (I'll go with the whole running my programs from command line-method that you kindly suggested).

Campbell Ritchie wrote:That has nothing to do with setters; the () represent invoking the constructor.

Thank you for enlightening me on what parentheses really are in this sort of context (object creation). However, the code was more of a representation of what I was attempting to do with object creation and use, and was in no way connected with getters and setters - again, sorry for the confusion (I probably should have made a note of that somewhere in the post).
_________________
Jeff Verdegan wrote:It means it can't be overridden.

Thank you for answering as to how final affects methods, I'll be sure to use it from this point forward (and I'll also be sure to learn more about it and when to use it exactly).
They're just methods, like any other, although by convention they retrieve or store state in an object. The only relation between them and the above is that to invoke a getter or setter, you have to have at some point created an object in which to store the data you're getting or setting.

Although I don't think this was intended, this really helped me to better understand getters and setters, thank you. (I do believe the intended effect of this message was to clear the difference in use of parentheses between creating an object and using getters and setters; the effect, however, was to help clarify that getters and setters are methods basically.)
_________________
Vineeth Menon wrote:From what I can see from your code

I'm not sure what that code snippet is supposed to tell you aside from the fact that I was checking if something would work (which it didn't), but I don't believe it reflects even half of what I do know about the Java language. O_o
you know things here and there, but it's not organised. I'd say you need to get a hold of programming concepts, like calling a method, or the different ways of writing the String parameter in the main method
This much is true though, I do need a firmer grasp on the concepts (like what the names of the parts of a code are). My code itself, however, is becoming very neat - but my knowledge truly is unorganized (something that I'm working on, and I'm actually able to write code more fluently now, thanks to having to rewrite my program 5 times now, not to mention rewriting it again for my friend (who my teacher essentially screwed over, but that's a story that will probably never be told by me)).
Try the Head first Java Programming Book, that is a great place to start.
Thanks, I'll try it out some time (probably over the summer, which I plan to spend furthering my knowledge in Java and perhaps even C#).


On a side note: I hope this is the kind of response that you guys find pleasant, as I tried to organize it better for easier reading.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:all I was saying was that editing variables by use of objects wasn't working, so I'll need to use getters and setters. I've actually found out why I can't use objects to edit variables in my program


Sorry, still no idea what you're talking about. I don't know what "by use of objects" is supposed to mean here. The variables that you'll modify with setter methods only exist inside of objects, but definition. So I don't know what you're talking about with the "(not) using objects" part. It might just be a terminology problem, or it might be a conceptual one.

Jeff Verdegan wrote:They're just methods, like any other, although by convention they retrieve or store state in an object. The only relation between them and the above is that to invoke a getter or setter, you have to have at some point created an object in which to store the data you're getting or setting.

Although I don't think this was intended, this really helped me to better understand getters and setters, thank you.


You're welcome. I hope things are gradually becoming clearer. I realize it can take a while to get your head around all these new concepts that get thrown at you at once. Usually what happens is you flounder without a clue for a while, and then one concept finally takes root in your brain, and that serves as a spark to help the others fall in line.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:It might just be a terminology problem, or it might be a conceptual one.

Personally, I think it's more terminology than grasping the concept that gets me... it's kind of awkward trying to explain something without know much of anything in terms of what terms are used.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:Is CamelCase a required or suggested rule?

As Campbell said, Java itself doesn't care unless you use a keyword by mistake; but the conventions he pointed you to are so pervasive as to be almost rules.

the reasons I write all my classes as lowercaseUppercase is because that's how my teacher does it, and it appeals to me. (My teacher actually told one of the students in my class that writing their class name as "Carwash" was incorrect, as it should be written "carWash" - "That's how it's supposed to be written", she said

Then I hate to say but, in this case, she's wrong. Practically any book, tutorial or example you find will name classes as I suggest - the reason being that they are then immediately distinguishable from methods and fields.

And if you think about it, if your teacher was correct, then a String would be a string, and an ArrayList an arrayList, wouldn't it? If you (or she) can find me a single standard Java class that starts with a lowercase letter, I'll eat my words and grovel unconditionally.

Winston
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've begun using CamelCase to differentiate class from method, and it helps a ton, thank you guys. I'll be posting another question sometime soon =p maybe
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!