File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes variable access issues - still disturbing.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable access issues - still disturbing...." Watch "variable access issues - still disturbing...." New topic

variable access issues - still disturbing....

Kevin Thompson
Ranch Hand

Joined: May 04, 2001
Posts: 237
Thanks for the help with the package topic! Today seems to be a worrisome day. There appears to be some gaping holes in my java knowledge. I think some of this comes from being entirely self taught?
I am trying to access the variable "x" in the constructor of the program called "KevinB".
What do I need to do?
This is my source for program "" ==>
import java.util.*;
public class KevinMain {
public static int x;
public static void main( String[] args ) throws IOException {
int a;
a = 1 + 2;
KevinB kb = new KevinB(a);

KevinMain km = new KevinMain();
int g;
g = km.methodTestA();
System.out.println("KevinMain - inside main method - value of g = "+g);

public int methodTestA(){
x = 1 + 9;
System.out.println("KevinMain methodTestA - value of x = "+x);
return x;
This is my source code for "" ==>
import java.util.*;
public class KevinB{
private int e;
public KevinB(int e){
System.out.println("KevinB Constructor - Value of e = "+e);
//How do I get access to value "x" from program KevinMain?
public void methodTestB(int e){
System.out.println("KevinB methodTestB - Value of e = "+e);
P.S. I have gone thru my piles of java books, and I don't really see a whole lot of material about this type of topic?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

//How do I get access to value "x" from program KevinMain?

Please, please use the "code" tags when posting code. After your 152 posts and 3 certifications, I am sure a ton of people asked you already. Regarding your question, since your variable "x" is declared public static in class KevinMain, you can access it as KevinMain.x, -- you don't even need an instance of KevinMain to do it. Whether this is a good programming practice depends on what you hold in that variable "x". I have rarely seen any use of public static members, unless they are declared as final public static references to immutable objects or primitive types.
[ March 07, 2003: Message edited by: Eugene Kononov ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Like Eugene, Im not sure what you want this program to do, but it sounds like you just want it to get the methodTestB in class KevinB to access the int x value. I cleaned it up and this is what it should look like to complete this. The program is pretty simple. I don't tend to relegate that it should be harder than it is but your idea may be.

The KevinB class should look something like this:

Hope this is what you were looking for.
peace out
I agree. Here's the link:
subject: variable access issues - still disturbing....
It's not a secret anymore!