Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

global variable

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
Samooramad
Asked:
Samooramad
  • 4
  • 3
1 Solution
 
CEHJCommented:
No you should use an accessor method

private String someString;

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

public String getSomeString() {
      return someString;
}
0
 
SamooramadAuthor Commented:
could i do the same with a list?
0
 
SamooramadAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
CEHJCommented:
>>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
 
SamooramadAuthor Commented:
>> 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
 
CEHJCommented:
Any point will do
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now