Results 1 to 7 of 7
  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default 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:

    Java Code:
    courseName = name;
    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.

    __________________________________________________ __
    in the second program when i call to the first a line something like
    Java Code:
    Gradebook mygradebook = new Gradebook
    what is all that saying exactly?

    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.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by hayden06f4i View Post
    Java Code:
    courseName = name;
    This is stating that the variable courseName is being assigned the variable name.
    Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)



    Quote Originally Posted by hayden06f4i View Post
    Java Code:
    Gradebook mygradebook = new Gradebook
    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)

  3. #3
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    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();

  4. #4
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    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;?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

  6. #6
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    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.

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by hayden06f4i View Post
    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;?
    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)).
    Java Code:
    public class Dog {
         String mBreed;
    
         public Dog() {
              super();
         )
         
         public Dog(String pBreed) {
              this();
              setBreed(pBreed);
         }
     
         public void setBreed(String pBreed) {
              mBreed = pBreed;
         }
    
    }
    Notice the setter method setBreed(String) sets the mBread variable to whatever is being passed in the variable pBreed.

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 06:17 PM
  2. Basic java help =)
    By Xycose in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 02:20 AM
  3. Some Basic file handling questions
    By alacn in forum New To Java
    Replies: 1
    Last Post: 06-21-2010, 11:24 PM
  4. Basic Image Questions
    By DaRancor in forum AWT / Swing
    Replies: 6
    Last Post: 11-22-2009, 12:20 AM
  5. Basic java
    By santa in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 10:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •