Mac Java Classes

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I had a quick question. I am going back to the begining of my Java for Dummies book to read over the section that talks about Class, method and so on so I can get a better understanding of what I am doing.

I understand this code and what it does but I want to understand the different elements.

System.out.println

is 'System' the 'Class'? and println the 'Method'? If I am right so far what is the 'Out', is it a SubClass of System?

This board has been great for me to get a better understanding of Java, Thank you.

-Lars
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
larswik said:
System.out.println

is 'System' the 'Class'? and println the 'Method'? If I am right so far what is the 'Out', is it a SubClass of System?
You're close. :)

Look at the call stack ...

System.out.println("Hello, world!");

println(String s) is obviously the method. But which object is it attached to?

Visually, that's pretty easy. It's directly attached to System.out ... but what is System.out? This is where it gets a bit tricky.

System is a static class. out is a static member of that class. You picked an odd question. :p

Perhaps it might help if you read the javadoc?

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html
 

Kunimodi

macrumors member
Sep 8, 2006
65
0
Ashland, OR, USA
larswik said:
System.out.println

is 'System' the 'Class'? and println the 'Method'? If I am right so far what is the 'Out', is it a SubClass of System?
Hi, Lars. Yes, System the class. out is a public static member (allowing you to freely access it from anywhere without instantiating System) in System and is of type PrintStream. PrintStream contains a method, println. Here is the JavaDoc for it:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#out

On a side note, PrintStream now contains the immensely useful printf() method that C programmers have enjoyed since Woodstock days. Finally. ;)
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
The reason that I selected that one is because it is all over the 'Java for Dummies' book that I am reading for every example. So I thought I would use that as my sample question.

That helped answer my question. System is a static class and out is a static member.

The book first talked about all this stuff and it was greek at first. When you start to write code from the book you see what is happening and things start to click. But I need to go back and really grasp that. I almost think the book would be better if you get into coding forst and then it goes over what Static, Class, Method and the other stuff ment.

Thanks.

-Lars
 

Kunimodi

macrumors member
Sep 8, 2006
65
0
Ashland, OR, USA
larswik said:
System is a static class and out is a static member.
You've got it now. However, I would like to point out one thing. A 'static class' in java is a pattern and not something explicitly provided by the language. In other words, you can't do:
Code:
public static class StaticClass
{
}
Instead, the pattern used is to make the constructor private:
Code:
public class StaticClass
{
    private StaticClass() { }
}
Because this precludes direct external instantiation, only static methods are initially available. I emphasize 'initially' because even now the class is not always a static class. It could e.g. follow the singleton pattern.