How can I call a variable from another class

hiIf I have two classes : one and twoIn class two I have a variable called : actionIn class one I want to check what is the value of action.How can I call action?
[197 byte] By [tleis] at [2007-9-30 20:25:31]
# 1

If the variable is public, you can just access it directly, as in the following example:

class MyClass2

{

public int action;

}

class MyClass1

{

public void someMethod()

{

MyClass2 myClass2 = new MyClass2();

myClass2.action = 7;

// ...

int theAction = myClass2.action;

}

}

However, it's bad form to have publicly accessible variables. You should use getter and setter methods to allow access to class properties, like this:

class MyClass2

{

private int action; // now nobody can access it directly

public int getAction() // callers have to go through this getter method

{

return action;

}

public int setAction(int newAction) // callers have to go through this setter method

{

action = newAction;

}

}

class MyClass1

{

public void someMethod()

{

MyClass2 myClass2 = new MyClass2();

myClass2.setAction(7);

// ...

int theAction = myClass2.getAction();

}

}

Using getters and setters allows you to provide "read only" variables (i.e. a getter but no setter) if you want to, and also allows you to hide the implementation of a variable (i.e. maybe every time getAction() is called we look up a value in a database or out of a file or something - the point is the caller doesn't have to know or care). Also, it allows us to enforce certain checks on new values in the setter method to ensure that the new value makes sense (for example, setAction() could reject any number less than zero, thereby ensuring that the value of action is always positive, if need be).

scorbett at 2007-7-7 1:10:05 > top of Java-index,Administration Tools,Sun Connection...
# 2

Thank you scorbett

what you told me worked fine, but my problem is that MyClass2 is an application by itself that I don't want to be executed.

Creating myClass2 as in the following:

[Code]

MyClass2 myClass2 = new MyClass2();

[/code]

executes myClass2.

Can I prevent the exectuion of MyClass2, or is there another way to call the variable (action)?

tleis at 2007-7-7 1:10:05 > top of Java-index,Administration Tools,Sun Connection...
# 3

This line:

MyClass2 myclass2 = new MyClass2();

doesn't really "execute" MyClass2, although it does cause the contents of MyClass2's default constructor to be run. Is that what you mean? If you mean that MyClass2 has a main() method that can be used to execute MyClass2 as an application, then no, the above line of code won't cause that to happen.

scorbett at 2007-7-7 1:10:05 > top of Java-index,Administration Tools,Sun Connection...
# 4

You wrote, "... MyClass2 is an application by itself that I don't want to be executed"

This is a puzzle and it must not be what you want as it is contradictory.

If you don't what MyClass2 to be executed, don't write it.

Surely you didn't mean that....

Basically there are three reasons why instance fields (class variables) are made private. 1) Many large scale systems are developed by many programmers who do not know what variable names the othre programmers are using. Making them private prevents one programmer from stepping on another programmer's code. 2) Security is improved when the class variables are encapsulated (made invisible from outside of the class. 3) It forces the programmer to write better code.

But, yes, as long as you are not using the code for production environments and you are just playing around, make the variables public and step on your own feet all you want. <grin>

MasonKelsey at 2007-7-7 1:10:05 > top of Java-index,Administration Tools,Sun Connection...