Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instantiating classes and the JVM

Status
Not open for further replies.

dbrb2

Instructor
Jul 19, 2004
121
GB
Hi,

I'm new yo java - apologies for silly questions. I'm getting a bit confused by the way classes are instantiated and called. In C++, to make use of a class I must create an instance of that class and then call methods within the class.

In Java, if for instance I have a class with a main method, I can run that code and the main method is run. However, I have not had to instantiate the class - is this done by the JVM?


In the example given on Suns website:


we look at an applet that responds to mouse events. We are told that the class must implement the mouslistner interface to work - fair enough. However, the neccesary methods seem to be called automatically - is this the JVM being helpful again?

Is the JVM doinjg al this instantiating and calling methiods as needed automatically 'behind the scenes' ?

Sorry for the enourmously long post,

Cheers,


Ben



 
You have to instantiate a class in Java too, to run its methods.
A distinction are 'static' methods, which don't use the Members of a class, and are therefore independent from a specific instance of the class.

'main' is such a method, because it must be called, before a class is created - in contrary main is creating the class.

Code:
class A
{
   // ...

   public static void main (String [] args)
   {
       A a = new A ();
       a.doSomething ();
   }
}

I'm not an applet-writer, but I know, they behave a bit different - they don't have a 'main', but you need to call 'init' or something.

seeking a job as java-programmer in Berlin:
 
A little addendum: If you want to access object functionality (as opposed to class functionality), an instance has to be created. Like this:

Code:
public class SomeClass
{
  public someFunction()
  {
    // do something
  }

  public static someOtherFunction()
  {
    // do something else
  }

  public static void main(String[] args)
  {
    someFunction();                 // this won't even compile
    someOtherFunction();            // this works, it's also static
    new SomeClass().someFunction(); // this works as well -
                                    // a SomeClass is instantiated here
  }
}

Regarding your other question: Things gets a little more complicated if you use AWT or Swing, the GUI toolkits in Java. Have a look at the Tutorials if you need more info than I'll provide here.

Basically, yes, the functions are called automatically. But you have to register your listener first, with addMouseListener() with the corresponding GUI element class. The AWT event dispatcher thread then takes care of redrawing windows (JFrames in Swing) and UI elements and also calls the mouse event listener's functions - it's somewhat like a callback function in C++. You register the reference (which is pretty similar to a pointer) to your listener object with the dispatcher, through the Component class, from which all Swing and AWT GUI classes are derived. And the dispatcher then looks, when a mouse event is incoming, where to distribute it.

Hope this helps

haslo@haslo.ch - www.haslo.ch​
 
Thanks - the use of static class methods for things such as 'main' makes things far clearer.

Cheers,


Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top