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 Accessing a valiable outside the class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a valiable outside the class" Watch "Accessing a valiable outside the class" New topic
Author

Accessing a valiable outside the class

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I have stored values in a List(public)...I want to use those values inside another class
without extending that class How can i?
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Originally posted by shalini gnana:
I have stored values in a List(public)...I want to use those values inside another class
without extending that class How can i?



Make it as a static one then you can access with
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I tried to read but its not giving me the content of the list in my paint class..am i proceeding correctly?Where do i went wrong?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

You have the list available with you. Why can't you try obtaining the iterator from the list as you have done in your Nodes class?

Please use the "Code" tags when pasting the java code. It will give a nice look and help the people much.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Please don't set up an Iterator as a field; always use a local variable. The reason is that it can throw an Exception if other objects access the list.

Don't give a field public access; use a getList() method instead.
Are you trying to get the details to appear when you call some Swing component or other? How are you calling the paintcomponent method? If you are trying to do it the usual way, overriding the method, you have got two tiny subtle errors which are very difficult to see. 1: Give the method protected access, as in the Component class. 2: There is a tiny spelling error; you ought to use C instead of c. Prefixing the method with the "@Override" annotation is very useful; it would have thrown a compiler error.
It is unusual to call System.out.println() from inside paintComponent; you would usually put Strings on the Component with the Graphics object.

You have the wrong format for comments. Either //..... or /*....*/. Not //**....**/. You would get a compiler error if you split that comment into two lines.

See whether those corrections improve things. CR
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
the elements are added to the list in a method...while calling the method from another class should i have to call the method where the elements are got appended or just the variable(variable is declared as class variable)...If the how to call the method,mear calling will do?

Thank you....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Not sure I quite understand your most recent question, but I shall try.

You can get at the List like this, and add elements:Or you can have a method in the Bar class like this:I think the second solution is much neater.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I have add elements to list in a method and iterated and printed in another method.When i called the list variable in another class its empty...What can i do to get those values in the list?Where do i went wrong?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Your two classes are completely confused.

The answer to your original question should have been "provide an accesor method" like this:That would however allow objects from other classes to do whatever they wish with your List.
I think the advice to make the List static was mistaken; you now have a single List which is shared between all instances of the class.
Have you sorted out the difference between paintComponent() which is used to set a display on a GUI component and System.out.print() which produces output on the command line or terminal?

I have already told you how to use methods to add to a List. In your latest example, you declare a field called list1 (bad name; you should be more specific) and then write list in your method. That suggests you have another variable, maybe even a local variable, which you are adding to. If it is a local variable, it will vanish altogether when the method completes. Also you are adding "node" which I can't understand.

For the iteration through the List and printing out its members, use a method which prints the List out.You ought to know how to traverse a Collection; look in the Java Tutorial (collections->interfaces->Collection interface) for more details.
[ January 05, 2008: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a valiable outside the class
 
Similar Threads
not getting list
Storing values in enum
double click on textbox showing some default values
How to use Cookie in Cactus
Updating data in the JTable