Solved

java Class FAQ

Posted on 2004-04-01
17
308 Views
Last Modified: 2013-11-23
i have a java program like below:

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class popUp extends JPanel implements ActionListener
{
      public popUp()
      {
            System.out.println("popUp");
      }

      public static void main(String[] args)
      {
            new popUp().createComponents();
      }
}

i have i question that is y i didn't call the popUp function but the popUp function will call by itself?

thanks
0
Comment
Question by:Kennywen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
17 Comments
 

Author Comment

by:Kennywen
ID: 10738575
if i use:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createComponents();
            }
        });

then the popUp will not run by itself ... why?

thanks
0
 
LVL 3

Expert Comment

by:concon
ID: 10738590
Hi Kennywen,

popUp() is the constructor of the class. and if your create new instance of class it runs.

also you can not call a non-static method from a static method. so you need to create an instance of popUp class in order to call another method from main.

Regards.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738595
I don't understand your question properly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kennywen
ID: 10738632
i mean the new popUp().createComponents(); will call the public popUp() function rite?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738655
It should.
0
 

Author Comment

by:Kennywen
ID: 10738667
javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createComponents();
            }
        });

what is the above function???
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738687
It causes the run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI.

Where are you writing that invokeLater () code?
0
 

Author Comment

by:Kennywen
ID: 10738704
in main method.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738720
You mean:

public static void main ( String args[] )
{
  SwingUtitlies.invokeLater ( .... ) ;

}

??

That will obviously not call popUp ().
0
 

Author Comment

by:Kennywen
ID: 10738739
sorry, why the  SwingUtitlies.invokeLater ( .... ) ; will not call popUp ()?

thanks
0
 
LVL 3

Assisted Solution

by:concon
concon earned 20 total points
ID: 10738757
because it is a method of a different class and popUp is only called when a new class instance is created.

running main does not call constructor of class.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 20 total points
ID: 10738763
Why should it?

It will call only if there is an instance of popUp right?

main () is a static method. You should not call SwingUtilities.invokeLater () where it is not to be called. Writing it there will do nothing. Which createComponents () method would it call? The one which popUp has? Well, but when this main () is running, it does not have an instance of popUp.
0
 

Author Comment

by:Kennywen
ID: 10738771
thank you and forgive my poor english.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738814
Glad to help ;-)
0
 

Author Comment

by:Kennywen
ID: 10738817
those method that call by the main method must be static rite?

is there anyway to create a non-static method from the main method without create an instance of it?

thanks
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738838
No. You need to make an object to call the non-static method. Otherwise, you will not be able to do it.

The reason why you were able to call createComponents from main () was that your main () is in the popUp class. Try putting the main () separately in a different class and use the same SwingUtilities.invokeLater () code - I don't think that it'll compile.
0
 

Author Comment

by:Kennywen
ID: 10738846
thanks again
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question