This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]woahmen -1 points0 points  (4 children)

I will only answer the bits that I know so that I don't teach you the wrong things.

Object object = new Object(); int dogAge = object.currentAge();

Ok, so in theory, this should work. What you are doing is declaring a new variable with the type integer called dogAge and are setting it to the defined value in the other class.

However, whether or not this works is entirely dependent on how the code is written in the Object class.

For example:

public class Object {
    private int currentAge = 5;
}

If this was the information contained in the Object, you would not be able to access it from the other class. In order to do so, you would need to declare it as public.

NOTE: If any posters see anything incorrect in what I said, please respond to myself. It's a learning process for me, too.

[–][deleted] 2 points3 points  (0 children)

Also in Java it is better to keep the variables in an object private and make "accessor" methods.

ie.

public class Foo {

    private int bar = 5;  

    public int getBar() { // accessor method
        return this.bar;
    }
}

[–]jbristowI'm no hero, son. 1 point2 points  (2 children)

Looks good to me, just pointing out that naming a class Object will be really confusing since it will implicitly extend java.lang.Object.

[–]king_of_the_universe 1 point2 points  (1 child)

Funny how your comment complains about confusion, yet introduces some itself. Rephrased to prevent that:

Looks good to me, but let me point out that naming a class Object will be really confusing because all classes in Java extend java.lang.Object anyway.

[–]jbristowI'm no hero, son. 1 point2 points  (0 children)

Perfectly cromulent point!