Results 1 to 6 of 6
- 10-15-2012, 07:06 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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 thanksLast edited by bbfunk; 10-15-2012 at 07:09 PM.
- 10-15-2012, 09:52 PM #2
Member
- Join Date
- Oct 2012
- Location
- Georgia
- Posts
- 7
- Rep Power
- 0
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.
- 10-16-2012, 12:45 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Simple Java questions from beginner (creating variable help)
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
- 10-16-2012, 02:21 AM #4
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
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 02:25 AM.
- 10-16-2012, 09:51 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Simple Java questions from beginner (creating variable help)
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:
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).Java Code:Dog hound
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.
This bit creates an object on the heap.Java Code:new Dog()
It also provides the reference to that object which can be assigned.
So this assigns that reference to your 'hound' variable.Java Code:hound = new Dog()
Please do not ask for code as refusal often offends.
- 10-16-2012, 01:10 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
beginner java programmer here with some questions!
By bbames_jond in forum New To JavaReplies: 2Last Post: 10-01-2012, 06:35 PM -
JAVA Beginner - Simple Program help
By Logik22 in forum New To JavaReplies: 13Last Post: 07-15-2011, 02:44 PM -
Java Beginner needs help with creating a Class
By BeginnerJava in forum New To JavaReplies: 7Last Post: 06-10-2011, 09:55 PM -
A few beginner questions about JavaFX vs normal Java.
By evanejk in forum New To JavaReplies: 6Last Post: 09-26-2010, 02:25 PM -
Java beginner, need some help on a simple tutorial? :D
By mootxico in forum New To JavaReplies: 1Last Post: 03-15-2009, 03:50 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks