Instantiating global object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Instantiating global object

  1. #1
    James Guest

    Instantiating global object


    I've run into a situation that I don't quite understand and was hoping somebody
    could help.

    I have created a simple class that I'm using in my main class. I tried declaring
    a global variable to this class and then instantiate it when Main is called.
    Ex:

    package myApp;
    import myApp.animal;

    public class JFrame extends javax.swing.JFrame {

    private animal myAnimal;

    public static void main(String args[])
    {
    myAnimal = new animal();
    }

    private void jButtonMouseClicked()
    {
    myAnimal.setX(100); // generates an exception error
    }
    }

    In the code above, if you click the button, a null exception is generated.
    My guess is because the object is not being instantiated even though I do
    so in "main".

    If I change the declared variable to read:
    private animal myAnimal = new animal();

    and delete the instantiation in "main", the code works.

    This is confusing to me because I thought you could declare a global class
    object and instantiate it elsewhere. For example, I could set "int x;" and
    then set its value in a method.

    Anyone help on explaining this one is greatly appreciated. Thanks!!!!

    Jaime

  2. #2
    Kent Guest

    Re: Instantiating global object


    James,

    It's hard to know for sure because you didn't post all your code but I think
    it is simply because your main() method is never called. main() methods are
    only called when you explicitly do so in your app or when you run a Java
    class from the command line.

    I would suggest you either:

    - put the initialisation in a constructor
    - make the variable static if possible and put the initialisation in a static
    block
    - declare and initialise the variable on the same line

    If you need more info let me know...

    Regards,
    Kent


    "James" <jfuhr@longaberger.com> wrote:
    >
    >I've run into a situation that I don't quite understand and was hoping somebody
    >could help.
    >
    >I have created a simple class that I'm using in my main class. I tried

    declaring
    >a global variable to this class and then instantiate it when Main is called.
    > Ex:
    >
    >package myApp;
    >import myApp.animal;
    >
    >public class JFrame extends javax.swing.JFrame {
    >
    > private animal myAnimal;
    >
    > public static void main(String args[])
    > {
    > myAnimal = new animal();
    > }
    >
    > private void jButtonMouseClicked()
    > {
    > myAnimal.setX(100); // generates an exception error
    > }
    >}
    >
    >In the code above, if you click the button, a null exception is generated.
    > My guess is because the object is not being instantiated even though I

    do
    >so in "main".
    >
    >If I change the declared variable to read:
    > private animal myAnimal = new animal();
    >
    >and delete the instantiation in "main", the code works.
    >
    >This is confusing to me because I thought you could declare a global class
    >object and instantiate it elsewhere. For example, I could set "int x;"

    and
    >then set its value in a method.
    >
    >Anyone help on explaining this one is greatly appreciated. Thanks!!!!
    >
    >Jaime



  3. #3
    markN Guest

    Re: Instantiating global object


    Additional info - if you don't make the variable static (I'm not saying you
    should) you will get an error when calling main with your code as is.

    While I am at it:
    1. animal should be Animal
    2. the variable isn't 'Global'.
    3. Get rid of the 'my' stuff.
    Try instead (including Kents suggestions)

    package jf.animal.ui;
    import jf.animal;

    public class AnimalView extends javax.swing.JPanel {

    private Animal animal;
    public AnimalView()
    {
    animal = new Animal();
    }
    public static void main(String args[])
    {
    //Create a Jframe and add view
    JFrame animalViewContainer = ....
    }

    private void jButtonMouseClicked()
    {
    myAnimal.setX(100); // generates an exception error
    }
    }


    This is better but the view shouldn't really be creating a new animal object
    - its controller should. But that is an advanced topic.

    "Kent" <kb@essential.com.au> wrote:
    >
    >James,
    >
    >It's hard to know for sure because you didn't post all your code but I think
    >it is simply because your main() method is never called. main() methods

    are
    >only called when you explicitly do so in your app or when you run a Java
    >class from the command line.
    >
    >I would suggest you either:
    >
    >- put the initialisation in a constructor
    >- make the variable static if possible and put the initialisation in a static
    >block
    >- declare and initialise the variable on the same line
    >
    >If you need more info let me know...
    >
    >Regards,
    >Kent
    >
    >
    >"James" <jfuhr@longaberger.com> wrote:
    >>
    >>I've run into a situation that I don't quite understand and was hoping

    somebody
    >>could help.
    >>
    >>I have created a simple class that I'm using in my main class. I tried

    >declaring
    >>a global variable to this class and then instantiate it when Main is called.
    >> Ex:
    >>
    >>package myApp;
    >>import myApp.animal;
    >>
    >>public class JFrame extends javax.swing.JFrame {
    >>
    >> private animal myAnimal;
    >>
    >> public static void main(String args[])
    >> {
    >> myAnimal = new animal();
    >> }
    >>
    >> private void jButtonMouseClicked()
    >> {
    >> myAnimal.setX(100); // generates an exception error
    >> }
    >>}
    >>
    >>In the code above, if you click the button, a null exception is generated.
    >> My guess is because the object is not being instantiated even though I

    >do
    >>so in "main".
    >>
    >>If I change the declared variable to read:
    >> private animal myAnimal = new animal();
    >>
    >>and delete the instantiation in "main", the code works.
    >>
    >>This is confusing to me because I thought you could declare a global class
    >>object and instantiate it elsewhere. For example, I could set "int x;"

    >and
    >>then set its value in a method.
    >>
    >>Anyone help on explaining this one is greatly appreciated. Thanks!!!!
    >>
    >>Jaime

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center