Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Simple Java questions from beginner (creating variable help)

  1. #1
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Simple Java questions from beginner (creating variable help)

    Hi all,

    I am trying to learn java and I am a complete beginner but I keep confusing myself, think I have read too much too soon.

    Basically I have this bit of code:

    Dog hound = new Dog();
    hound.moveLeft(3);

    Am I right in saying here that:
    Dog = Class
    hound = Variable (that stores the new object)
    = new Dog() = Create's a new instance of the class to be stored under Hound

    IF so, why do we not just say "hound = new Dog()" or "Dog = new hound()".

    I am confusing myself big time here and my study books don't go any further on this.

    Hope someone can help thanks
    Last edited by bbfunk; 10-15-2012 at 08:09 PM.

  2. #2
    Abizzar is offline Member
    Join Date
    Oct 2012
    Location
    Georgia
    Posts
    7
    Rep Power
    0

    Default Re: Simple Java questions from beginner (creating variable help)

    You have it correct. Now..
    If you did hound = new Dog();
    ^^^ you never defined hound that's like doing a = 100 without initializing a.
    It's basically just a rule of java so you have to follow it it becomes less complex when you get used to it trust me.

  3. #3
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Simple Java questions from beginner (creating variable help)

    Quote Originally Posted by Abizzar View Post
    You have it correct. Now..
    If you did hound = new Dog();
    ^^^ you never defined hound that's like doing a = 100 without initializing a.
    It's basically just a rule of java so you have to follow it it becomes less complex when you get used to it trust me.
    So to define the variable, we put the class infront of it? i.e Class Variable = class()

    So this statement is saying:
    Dog hound = new Dog();
    In the class Dog, create a variable called hound = this new variable will contain an instance/object of the class Dog(default datatype)

    Is this correct?

    Thanks

  4. #4
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Simple Java questions from beginner (creating variable help)

    Though Dog is a class, it is also a type; like int or double. Saying
    Dog hound;
    Is saying create a variable called hound of type Dog.

    You cannot just say:
    hound = new Dog();
    because what type of variable is hound? It needs to be defined as a certain type. Sure it is a new instance of the class Dog(), but that is the equivelant of saying:
    variableName = 12;
    You must define the type of variableName before you can give it a value.

    You can define hound as different types other then Dog, as you will find out later in your reading. When you start learning more about inheritance and what not, you will have a more thorough understanding of why this needs to be declared, and should not just be a default in the language. Because technically you could declare it as:
    Object hound = new Dog();
    because all objects, by default, inherit the attributes of method Object.
    Last edited by penguinCoder; 10-16-2012 at 03:25 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Simple Java questions from beginner (creating variable help)

    Quote Originally Posted by bbfunk View Post
    this new variable will contain an instance/object of the class Dog(default datatype)
    Not quite.

    'hound' is a reference (and this is quite important).
    It does not "contain an object".
    It points to an object on the heap (the place where all objects live).

    So, to break down the line:
    Java Code:
    Dog hound
    This declares a reference variable called hound. That reference can be used to point to objects on the heap that are Dog classes (or subclasses of Dog).
    The compiler uses this to ensure type safety. That is, to ensure you don't try and do something with a 'hound' reference that is not allowed by the Dog class.
    Java Code:
    new Dog()
    This bit creates an object on the heap.
    It also provides the reference to that object which can be assigned.
    Java Code:
    hound = new Dog()
    So this assigns that reference to your 'hound' variable.
    penguinCoder likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    bbfunk is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Simple Java questions from beginner (creating variable help)

    Fantastic. Understand it much better thank you!

Similar Threads

  1. beginner java programmer here with some questions!
    By bbames_jond in forum New To Java
    Replies: 2
    Last Post: 10-01-2012, 07:35 PM
  2. JAVA Beginner - Simple Program help
    By Logik22 in forum New To Java
    Replies: 13
    Last Post: 07-15-2011, 03:44 PM
  3. Java Beginner needs help with creating a Class
    By BeginnerJava in forum New To Java
    Replies: 7
    Last Post: 06-10-2011, 10:55 PM
  4. Replies: 6
    Last Post: 09-26-2010, 03:25 PM
  5. Replies: 1
    Last Post: 03-15-2009, 04:50 PM

Posting Permissions

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