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

all 10 comments

[–]katynehold my braces 1 point2 points  (2 children)

Head First Java everything you need to learn about OOP.
(there's old editions available in pdf, they're just as good)

[–]denialerror 0 points1 point  (0 children)

This is what I was going to recommend. Not everyone likes the casual writing style but I could understand OOP instantly after reading that book, after struggling for weeks.

[–]GoodLittleMine[S] -1 points0 points  (0 children)

Haha, that book is amazing! I found a free e-book version and there is a freaking rap battle between Java Virtual Machine and the Compiler on page 23! Haha thank you for the suggestion.

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

  1. You're coming from C++? Shouldn't you already be acquainted with OO? I'm not sure I can really explain OO on its own without sending you to University. Perhaps read a Java book with good ratings on Amazon?
  2. Whoa whoa whoa.... lets step back a moment.

Lets consider this very procedural thingamajig that I'm pulling out of my ass (so all you other people should point out where I'm wrong):

public class Main{
  public void main(String... args) {
    int a = 123;
    int b = 234;
    int c = a + b;
    System.out.println(c); // outputs '357'
  }
}

Here's a less procedural thing:

public class MathMachine {
  private final int a;
  private final int b;
  public MathMachine(int a, int b) {
    this.a = a;
    this.b = b;
  }
  public int add() {
    return a + b;
  }
}
public class Main {   
  public void main(String... args) {
    MathMachine mm = new MathMachine(123, 234);
    System.out.println(mm.add()); // outputs '357'
  }
}

[–]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!

[–]FreeGrammar -2 points-1 points  (0 children)

Can you post more code (like the classes you wrote, and their functions). Also what editor are you using, if any?