File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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 "KevinMain.java" ==>
import java.io.*;
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);
kb.methodTestB(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 "KevinB.java" ==>
import java.io.*;
import java.util.*;
public class KevinB{
private int e;
public KevinB(int e){
System.out.println("KevinB Constructor - Value of e = "+e);
//THE GOAL IS TO ACCESS VARIABLE "X" AT THIS POSITION:
//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);
}
}
thanks!
Kevin
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

//THE GOAL IS TO ACCESS VARIABLE "X" AT THIS POSITION:
//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.
Eugene.
[ 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: http://aspose.com/file-tools
 
subject: variable access issues - still disturbing....