Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2012, 05:27 PM   PM User | #1
rubbed
New Coder

 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
rubbed is an unknown quantity at this point
Simple Java questions from beginner (class/object)

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

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

hound.moveLeft = Message Send to tell the object referenced by the variable hound to move left.

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

Hope someone can help thanks
rubbed is offline   Reply With Quote
Old 10-15-2012, 06:13 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is pretty much all correct. Dog() itself doesn't create a new instance, its the new keyword that does so. Dog() is the datatype and constructor signature it will use (default in this example).
I don't like the terminology for the message on hound.moveLeft (message isn't quite what I would interpret of this). What would be probably better for an explanation is that moveLeft is invoked on the instance of Dog "hound", and given the value of 3. Invoke makes more sense than message, and it sounds more awesome.
Fou-Lu is offline   Reply With Quote
Old 10-15-2012, 07:06 PM   PM User | #3
rubbed
New Coder

 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
rubbed is an unknown quantity at this point
Hi,

Thanks!

I just used the moveLeft as an example and I agree with you there.

So basically, when we want to create a new variable to store a class into we do it like this:

Class Name | Variable Name | = CLASSNAME()

ie.
If I wanted to create a new variable called puppy to reference the Class Dog as an object:
Dog puppy = dog();

Or if our class was called Animal:
Animal puppy = animal();

So it is essentialy saying left to right:
[In the class Animal, create a variable called puppy] = [store class animal as a new object to variable puppy (default datatype)]

Is this correct?
I am just a little confused why we just don't say "puppy = animal ()" but hopefully if the above statement is true I'll understand better.

Thanks again

Last edited by rubbed; 10-15-2012 at 07:09 PM..
rubbed is offline   Reply With Quote
Old 10-15-2012, 07:24 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Not unless the functions dog/animal are declared, in scope of what is calling this code, and return the type Dog or Animal. You require the new keyword to construct a new object.
So using this:
PHP Code:
class Test
{
    public static 
void main(String[] argv)
    {
        
Dog puppy dog();
    }

Would require the following in class Test:
PHP Code:
public Dog dog()
{
    return new 
Dog();  // or get Dog from somewhere.

Fou-Lu is offline   Reply With Quote
Old 10-15-2012, 07:42 PM   PM User | #5
rubbed
New Coder

 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
rubbed is an unknown quantity at this point
Thanks for that got a better understanding of it now cheers!
rubbed is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:18 PM.


Advertisement
Log in to turn off these ads.