Results 1 to 6 of 6
  1. #1
    Winarto is offline Member
    Join Date
    Jan 2008
    Location
    Jakarta
    Posts
    6
    Rep Power
    0

    Question The different of static void,protected,and void in methods?

    I don't understand what is the different of static void, protected, or void in method.

    I don't know when can I use the static void, or void?

    for example :
    public class C extends JFrame {
    public C() {
    initComponents();
    }

    protected void initComponents() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //i dont understand
    setSize(400, 400);
    JTextArea textArea = new JTextArea();
    textArea.setText("Press the mouse button...");
    }
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new C().setVisible(true);
    }
    });
    }
    }
    why can we use this 'setDefaultCloseOperation' this time,without create an object first.
    may be someone can explain to me?
    Thank you...

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hello Winarto
    Quote Originally Posted by Winarto
    I don't understand what is the different of static void, protected, or void in method.
    A static modifier means that this field or method is the only one that will ever be created. That means that you do not have to create it because it already exists. A static member can be used anywhere in your program, by referring to it's class name. For example:
    Java Code:
    int test = Integer.parseInt("10");
    The word void means "an empty space". So that means that a void method returns nothing.
    Quote Originally Posted by Winarto
    why can we use this 'setDefaultCloseOperation' this time,without create an object first.
    You are extending your class, C, from JFrame. That means that C inherits all the inheritable members of the JFrame class and now you can call then as if you created them. A class defines how an instance of it should behave. So technically, "this" is an instance of C. Try this in your constructor before initComponents():
    Java Code:
    this.setTitle("I'm an instance of C.");
    I hope this helped.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Well said tim. Only thing, Winarto, the access-specifer "protected" is a way to determine how it can be accessed. You know "public"? , which can be accessed anywhere. "private" cannot be accessed. Well, protected is one of these. protected methods can only be accessed by subclasses. For example, if we have a method protected int getSum(), if we extend the class that this method resides in, this subclass can use getSum(). But if we were to just make another class, we couldn't call getSum().

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Oh, and for informational purposes, there is one more of these access-specifiers: "package". This obviously means that only classes that are within the same package of the class can use it. Ex:
    Java Code:
    [B]package pack;[/B]
    public class Example
    {
         //Constructor
    
         package int getSum()
         { //code  }
    }
    If we were to have a class:
    Java Code:
    [B]package pack;[/B]
    public class ExampleTester
    {
         public static void main(String[] args)
         {
              Example e = new Example();
              int sum = e.findSum(); //OK, this class is in the same package as          Example
         }
    }
    }

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Nice

    Quote Originally Posted by gibsonrocker800
    only classes that are within the same package of the class can use it. Ex:
    Java Code:
    package pack;
    public class Example{
        //Constructor
        package int getSum() {
             //code
        }
    }
    Thanks gibsonrocker800. I didn't see that in my book. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Quote Originally Posted by tim View Post
    Thanks gibsonrocker800. I didn't see that in my book. :D
    Ah! I'm glad i could help you man!

Similar Threads

  1. Java Native Access (JNA) return types of void *
    By burnumd in forum Advanced Java
    Replies: 5
    Last Post: 01-15-2010, 01:09 AM
  2. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 08:57 PM
  3. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  4. is void a type?
    By mary in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •