Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-18-2008, 07:17 AM
Member
 
Join Date: Jan 2008
Location: Jakarta
Posts: 6
Rep Power: 0
Winarto is on a distinguished road
Send a message via Yahoo to Winarto
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...
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-18-2008, 10:34 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default
Hello Winarto
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:
Code:
int test = Integer.parseInt("10");
The word void means "an empty space". So that means that a void method returns nothing.
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():
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-24-2008, 05:06 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
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().
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-24-2008, 05:09 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
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:
Code:
package pack;
public class Example
{
     //Constructor

     package int getSum()
     { //code  }
}
If we were to have a class:
Code:
package pack;
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
     }
}
}
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-24-2008, 10:02 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default Nice
Originally Posted by gibsonrocker800
only classes that are within the same package of the class can use it. Ex:
Code:
package pack;
public class Example{
    //Constructor
    package int getSum() {
         //code
    }
}
Thanks gibsonrocker800. I didn't see that in my book.
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-25-2008, 12:53 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default
Originally Posted by tim View Post
Thanks gibsonrocker800. I didn't see that in my book.
Ah! I'm glad i could help you man!
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 06:41 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org