Solved

Initializing new variable for entire class

Posted on 2006-11-21
8
192 Views
Last Modified: 2010-08-05
Hello!

I have a variable like this:

Server srv = new Server(5154);

I want this srv variable to be accessible from all methods of my class...
Server is another class...

I'm on another class using Server class...

I want to use this srv variable for example in method JButton1 Click and on JButton2 Click.

Thanks
0
Comment
Question by:CSecurity
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17990336
Just make it an instance variable of your class. It will be visible from every method
0
 
LVL 92

Expert Comment

by:objects
ID: 17990355
public class MyClass
{
   public static Server srv = new Server(5154);

   .....




You can then access it using:

Server srv = MyClass.srv;
0
 
LVL 17

Author Comment

by:CSecurity
ID: 17990362
Look.

Assume I have 2 methods Button1 Clicked and Button2 Clicked.

When I press Button1 the variable gets initilized.

And I want to initilaize it in a way that I can call it from Button2.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17990379
void buttonOneClicked() {
  if (srv == null) {
      srv = new Server(5154);
  }
}
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 17990388
the other way twould be to make it a member var or your class, and have both listeners as inner classes of that class

public class MyClass
{
   public Server srv;

   .....

   button1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         srv = new Server(5154);
         ...
      }});

   button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         srv.doSomething();
         ...
      }});
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 17990394
public class MyClass
{
   private Server srv = new Server(5154);
0
 
LVL 17

Author Comment

by:CSecurity
ID: 17990405
I get error on this line:
srv = new Server(5154);
non-static variable srv cannot be referenced from a static context
0
 
LVL 92

Expert Comment

by:objects
ID: 17990428
If you're calling it from a static method then you'll need to make it static as i showed in my first post
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

631 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