Solved

global variable

Posted on 2004-04-19
7
321 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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