Java Classes

Discussion in 'Mac Programming' started by larswik, Sep 27, 2006.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
     
  2. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #2
    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
     
  3. macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #3
    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. ;)
     
  4. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    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
     
  5. macrumors member

    Joined:
    Sep 8, 2006
    Location:
    Ashland, OR, USA
    #5
    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.
     

Share This Page