Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-18-2008, 07:17 AM
Member
 
Join Date: Jan 2008
Location: Jakarta
Posts: 6
Winarto is on a distinguished road
Send a message via Yahoo to Winarto
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
Sponsored Links
  #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: 334
tim is on a distinguished road
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:
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():
Code:
this.setTitle("I'm an instance of C.");
I hope this helped.
__________________
If your ship has not come in yet then build a lighthouse.
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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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: 334
tim is on a distinguished road
Nice
Quote:
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.
__________________
If your ship has not come in yet then build a lighthouse.
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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Quote:
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
Sponsored Links
Reply


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

vB 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 0 04-03-2008 06:12 PM
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 +3. The time now is 07:40 PM.


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