Results 1 to 6 of 6
- 01-18-2008, 06:17 AM #1
Member
- Join Date
- Jan 2008
- Location
- Jakarta
- Posts
- 6
- Rep Power
- 0
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...
- 01-18-2008, 09:34 AM #2
Hello Winarto
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:
Originally Posted by Winarto
The word void means "an empty space". So that means that a void method returns nothing.Java Code:int test = Integer.parseInt("10");
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():
Originally Posted by Winarto
I hope this helped.Java Code:this.setTitle("I'm an instance of C.");Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-24-2008, 04:06 AM #3
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().
- 01-24-2008, 04:09 AM #4
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:
If we were to have a class:Java Code:[B]package pack;[/B] public class Example { //Constructor package int getSum() { //code } }
}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 } }
- 01-24-2008, 09:02 AM #5
Nice
Thanks gibsonrocker800. I didn't see that in my book. :D
Originally Posted by gibsonrocker800 Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-24-2008, 11:53 PM #6
Similar Threads
-
Java Native Access (JNA) return types of void *
By burnumd in forum Advanced JavaReplies: 5Last Post: 01-15-2010, 12:09 AM -
Why methods in an interface cannot be static?
By cbalu in forum Advanced JavaReplies: 2Last Post: 12-12-2007, 07:57 PM -
Static methods
By Java Tip in forum Java TipReplies: 0Last Post: 11-04-2007, 05:56 PM -
is void a type?
By mary in forum New To JavaReplies: 3Last Post: 08-01-2007, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks