PDA

View Full Version : Java Classes




larswik
Sep 28, 2006, 12:36 AM
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
Sep 28, 2006, 12:46 AM
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
Sep 28, 2006, 12:51 AM
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
Sep 28, 2006, 12:58 AM
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
Sep 28, 2006, 01:11 AM
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:
public static class StaticClass
{
}

Instead, the pattern used is to make the constructor private:
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.