Solved

global variable

Posted on 2004-04-19
7
315 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now