Solved

java Class FAQ

Posted on 2004-04-01
17
303 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
  • 8
  • 7
  • 2
17 Comments
 

Author Comment

by:Kennywen
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
I don't understand your question properly.
0
 

Author Comment

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

Expert Comment

by:mayankeagle
Comment Utility
It should.
0
 

Author Comment

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

what is the above function???
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
in main method.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You mean:

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

}

??

That will obviously not call popUp ().
0
 

Author Comment

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

thanks
0
 
LVL 3

Assisted Solution

by:concon
concon earned 20 total points
Comment Utility
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:
mayankeagle earned 20 total points
Comment Utility
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
Comment Utility
thank you and forgive my poor english.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Glad to help ;-)
0
 

Author Comment

by:Kennywen
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
thanks again
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now