Solved

Initializing new variable for entire class

Posted on 2006-11-21
8
188 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

816 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

8 Experts available now in Live!

Get 1:1 Help Now