Solved

global variable

Posted on 2004-04-19
7
338 Views
Last Modified: 2010-03-31
hey everyone
I have a variable I want to use in 2 classes ( or two files to be exact) is it possible by just declaring it public?
can i access it normally?
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 10863631
No you should use an accessor method

private String someString;

public void setSomeString(String someString) {
      this.someString = someString;
}

public String getSomeString() {
      return someString;
}
0
 

Author Comment

by:Samooramad
ID: 10863705
could i do the same with a list?
0
 

Author Comment

by:Samooramad
ID: 10863737
also i would want to access its elements one at a time. does that change anything? are they accessed with a n index [i] like an array?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10863774
>>could i do the same with a list?

Yes. If your method is called getList then you'd do

Object o = getList().get(0); // get first element

0
 

Author Comment

by:Samooramad
ID: 10863856
>> public String getSomeString() {
     return someString;

does it make any difference where i put the method?
does somestring (or my list rather) need to be declared at a certain point in my code?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10863956
Any point will do
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10864193
8-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

739 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