Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

java Class FAQ

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
Kennywen
Asked:
Kennywen
  • 8
  • 7
  • 2
2 Solutions
 
KennywenAuthor Commented:
if i use:

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

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

thanks
0
 
conconCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
I don't understand your question properly.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
KennywenAuthor Commented:
i mean the new popUp().createComponents(); will call the public popUp() function rite?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
It should.
0
 
KennywenAuthor Commented:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createComponents();
            }
        });

what is the above function???
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
KennywenAuthor Commented:
in main method.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You mean:

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

}

??

That will obviously not call popUp ().
0
 
KennywenAuthor Commented:
sorry, why the  SwingUtitlies.invokeLater ( .... ) ; will not call popUp ()?

thanks
0
 
conconCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
KennywenAuthor Commented:
thank you and forgive my poor english.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Glad to help ;-)
0
 
KennywenAuthor Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
KennywenAuthor Commented:
thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 8
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now