LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2011, 05:25 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
how to set up a global class in JAVA


Hi all,
I hope someone can help me out. I've had to do some code in java, a language I'm very much unfamiliarly with so please excuse my incorrect use of terms. The basic outline of my problem is I create a class object as a local within a swing button function it works fine. If I create it as a global ( with I think I need to do ) within main, then prototype it with the other swing objects at the bottom of the file when it is called it causes a host of problems. I think the easiest way is to show it. So I declare the class at the top of my code
Code:
class GameTextData
{
    private String Namestr;

    public void setgamenamestring(String st)
    {

       Namestr = st;

    }

}
I then make the object with in the main loop
Code:
 public static void main(String args[]) {

    
        GameTextData MainVariableList = new GameTextData();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GameDevelopment().setVisible(true);
            }
        });
    }
I then try to call it from my button handler (which netbeans has placed above main) as follows

Code:
  private void jsetupdoneButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:

        


        //what to do with game name entry
        if( jGameNameField.getText().equals("") )
        {
            System.out.println("error please enter game name");
            return;
        }
        else System.out.println(jGameNameField.getText());


        MainVariableList.setgamenamestring( jGameNameField.getText() );

        //test it
        System.out.println( MainVariableList.Namestr );

    }
this reports an error of
Code:
cannot find symbol
symbol  : variable MainVariableList
location: class GameDevelopment
        MainVariableList.setgamenamestring( jGameNameField.getText() );
I can see that netbeans shows prototypes at the bottom of the main file, so I try to copy this and prototype my variable there, if I do this it give a private access for the variable in the class,
Code:
Namestr has private access in GameTextData
        System.out.println( MainVariableList.Namestr );
if I take off private for my string it will compile however when I call the button function it gives a dozen errors.

Can anyone advise? Thanks in advace
 
Old 05-10-2011, 03:10 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by knobby67 View Post
If I create it as a global ( with I think I need to do ) within main

I then make the object with in the main loop
Code:
public static void main(String args[]) {
    GameTextData MainVariableList = new GameTextData();
}
I then try to call it from my button handler (which netbeans has placed above main) as follows

Code:
private void jsetupdoneButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    MainVariableList.setgamenamestring( jGameNameField.getText() );
}
Code:
cannot find symbol
symbol  : variable MainVariableList
You can't create a "global" variable in Java simply by declaring it in your main function. What you're trying to do is the equivalent of something like this in (say) C:

Code:
int main(void)
{
    int x = 2;
    foo();
}

void foo(void)
{
    printf("%d\n", x);
}
But you can't access `x' from `foo', because it's local to main(). Similarly, the MainVariableList you've created is local to main() and can't be accessed from anywhere else.

Unfortunately, in Java there's no way to create a global variable that's outside of any class. Instead, make a class that represents your program, and make all the data you want "global" as static members of that class, like so:

Code:
public class MyProgram {
    public static GameTextData MainVariableList = new GameTextData();
}
Then you can access it from anywhere in your program like so:

Code:
MyProgram.MainVariableList.setgamenamestring(whatever);

Last edited by JohnGraham; 05-10-2011 at 03:13 PM.
 
1 members found this post helpful.
Old 05-11-2011, 01:55 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thank you Sir that worked! I'm a low level C / Assembler programmer so am finding using JAVA a bit tricky to get my head around. Lots of the bits like what you've helped me out with I can't find in any web documentation or tutorials.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set class path for java in ubuntu 9.10 kumar116 Linux - Software 2 05-24-2010 04:33 PM
How to set mail.jar(JavaMail) and mysql-connector-java.jar in Linux class path Terry56 Linux - Software 3 01-19-2010 09:06 AM
how to set class path in java on arm board in linux srinivas1049 Linux - Embedded & Single-board computer 0 04-21-2009 08:06 AM
Java: what's the difference between an inner class and a static inner class? irey Programming 6 01-28-2009 03:34 AM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration