cworner1
cworner1

Reputation: 451

Confused over simple java syntax - Novice programmer

I'm fairly new to java. I'm reading up on it and learning as I go along. I was hoping somebody could explain something to me. I looked up the implementation of the java library file in question but it didn't really solve much.

Let me start with an example of code:

System.out.println();

From what I understand this calls a method in the System class - println(). Whatever is in the parentheses are passed to the method as arguments (if that's the correct word for it). This I understand.

I also understand...

System.out.println(SomeMethod(SomeMethodAgain(x)));

.. this code returns a variable from "SomeMethodAgain(x) and passes it to "SomeMethod(), and then the results from SomeMethod() are then passed to the println() method.

But here's the question... I've seen a line of code that returns a variable of the "Dimension" type:

Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();

...now I could understand the code if it was just Toolkit.getDefaultToolKit(). but then it has the extension ".getScreenSize". So how does this command work? Is there a method within a method?? (I didn't think that was possible)??

Upvotes: 2

Views: 133

Answers (3)

DerMike
DerMike

Reputation: 16190

You can chain your method calls.

Toolkit.getDefaultToolkit()

return one Toolkit, that is then called getScreenSize() on. The value of this expression is the result of the last call.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499860

This line:

Toolkit.getDefaultToolkit().getScreenSize();

is equivalent to:

Toolkit tmp = Toolkit.getDefaultToolkit();
tmp.getScreenSize();

It's calling an instance method called getScreenSize() where the instance it's called on is the one returned from the static method Toolkit.getDefaultToolkit().

(It's not actually returning the instance; it's returning a reference to the instance, but one thing at a time...)

Upvotes: 4

NPE
NPE

Reputation: 500197

All this means is that Toolkit.getDefaultToolkit() returns an object. The object has a method called getScreenSize().

Upvotes: 3

Related Questions