Results 1 to 7 of 7
Thread: VERY basic Java questions
- 11-10-2010, 06:14 PM #1
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
VERY basic Java questions
So im 3 weeks into Java "How to Program" course, have made a few things, some things i understand very well, (selection statements, repetition statements), but obviously alot of things i don't. wondering if someone could re-explain something to me.
Contructors/set/get
like an example:
what is that saying exactly? is the name to the right of the = sign a java word, or am i naming it name, or....i guess the concept i have of constructors and set/get is off.Java Code:courseName = name;
__________________________________________________ __
in the second program when i call to the first a line something like
what is all that saying exactly?Java Code:Gradebook mygradebook = new Gradebook
my mind makes me have to know "how" and "why" things work, not just THAT they work, and i don't understand this. I know that i have made a class called Gradebook, and somehow im referencing that, but which word in that line is doing what?
You guys have already been a huge help to me in the first few weeks, and i really appreciate it.
- 11-10-2010, 06:21 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
This is stating that the variable courseName is being assigned the variable name.
Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)
Well, if you add the "();" to the Gradebook so you have "new Gradebook();", this is creating an instance of the class Gradebook and assigning it to the variable mygradebook.
Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 11-10-2010, 06:23 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
I'm not exactly sure what you're asking on your first question, but as for the second one, it's actually quite simple. I'll answer this with an example that I'm sure you've come across before (at least I did after 3 weeks).
Say that"Gradebook" is a class, and "mygradebook" is a student within that class. I'm going to rephrase it for easier understanding.
Gradebook ryan = new Gradebook();
What this is saying, essentially, is that I'm creating a new student within my gradebook that I can assign values to. To the right of the = sign you see "new Gradebook();". This is essentially telling Java to create a new "student". Here's a few more examples, let me know if you need more help.
Students john = new Students();
Birds cardinal = new Birds();
Countries peru = new Countries();
- 11-10-2010, 06:48 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
thanks to both of you guys, i still need more time programming for it to click. i understand what the point is, especially with Bari, your simplified example was a big help.
stormy, so a really dumb question, but what is the point of doing this then?
courseName = name;?
- 11-10-2010, 07:20 PM #5
Recommended reading: JavaRanch Campfire - Cup Size: a Story About Variables and its follow up JavaRanch Campfire - Pass By Value, Please
- 11-10-2010, 07:54 PM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 119
- Rep Power
- 0
thanks Kevin, i read both of those fully, it did make a couple things clear, the line
Gradebook mygradebook = new Gradebook (); now makes more sense.
i appreciate it guys, thank you.
- 11-10-2010, 08:09 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Its used to store whatever you currently have in variable name in the variable courseName. It's used a lot, especially in setter methods, though I've seen it done... um, everywhere I guess..., but set methods are a good example. It's usually done to increase the scope of the value in the variable being set to.
Example of a Setter Method(setBreed(String)).
Notice the setter method setBreed(String) sets the mBread variable to whatever is being passed in the variable pBreed.Java Code:public class Dog { String mBreed; public Dog() { super(); ) public Dog(String pBreed) { this(); setBreed(pBreed); } public void setBreed(String pBreed) { mBreed = pBreed; } }
Similar Threads
-
Basic Java help, AIM?
By jkswebsite in forum New To JavaReplies: 4Last Post: 07-11-2012, 06:17 PM -
Basic java help =)
By Xycose in forum New To JavaReplies: 1Last Post: 06-30-2010, 02:20 AM -
Some Basic file handling questions
By alacn in forum New To JavaReplies: 1Last Post: 06-21-2010, 11:24 PM -
Basic Image Questions
By DaRancor in forum AWT / SwingReplies: 6Last Post: 11-22-2009, 12:20 AM -
Basic java
By santa in forum New To JavaReplies: 5Last Post: 11-16-2009, 10:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks