Solved

Initializing new variable for entire class

Posted on 2006-11-21
8
185 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
  • 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17990379
void buttonOneClicked() {
  if (srv == null) {
      srv = new Server(5154);
  }
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert ArrayList of custom objects to JSON 4 53
powerN  challenge 3 47
topping3 challenge 14 50
Problem to start Neon 20 53
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

16 Experts available now in Live!

Get 1:1 Help Now