• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i acess variable of a class through array object  RSS feed

 
Asish Dash
Greenhorn
Posts: 7
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Ar // creation of object
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int i;
int j=10;
Ar a[]=new Ar[3];
a[1]=new Ar();
a[1].j=40;
System.out.println("Hello World!"+a[1].j);
System.out.println("Hello World!");
while i compile this error message shows variable j not found.can i acess class variable ,methods through array object what i did in above programme.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asish Dash,

Make j a global variable and then you can access it.
 
Asish Dash
Greenhorn
Posts: 7
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you mind to explain it in details??
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asish Dash,

I too am a beginner in java, so I don't know too much in detail. I know that we won't be able to access a method's local variable.

From what ever I studied, you are creating a new object and trying to access the main method's local variable which is not possible in java whereas a global variable, you can access it with different objects as it is created with every object you create.
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asish,

When posting code here you should UseCodeTags which you can do manually or just hit the Code button in the editor menu to insert the tags. I've copied, correctly indented, and added in the missing closing parenthesis for you just to show you how much nicer it looks.

As Partheban has said, the reason you are getting a compiler error is because your variable int j has scope only within the main() method but you are trying to access it in the scope of the Ar class. In order for you to access in the way that you want here a[1].j then you need to make j a member of Ar class. For example, here's my modifications to your class (I've removed some superfluous code to simplify things too)

An important point here is that I have not made the j variable global. I have made it a field of the class Ar.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote: . . . I have not made the j variable global. . . .
But giving it public access makes it as good as global.

Nobody should ever use public fields, except for global constants.
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But giving it public access makes it as good as global.

You are right of course. Although we still need an instance of the Ar class to get hold of the j variable there's nothing preventing us from doing that from anywhere in the application so in that sense it is global. I had started to write out some of the nasty gotcha's with this approach and also if we were to make it public and static but it was getting out of hand as there were just too many and I don't really think getting into it in this thread is going to help the OP much at all apart from just add more confusion to the mix. Besides, I'm sure it's been discussed before. (I haven't done a search to check)
Campbell Ritchie wrote:Nobody should ever use public fields, except for global constants.

I agree. But for the purposes of demonstrating the scope rules the OP was having difficulty with I deliberately didn't want to cloud the issue by throwing in a bunch of other "best practice" rules too.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Make j a global variable and then you can access it.

Actually, like Campbell, I have to disagree with that advice very strongly. Global variables are not a good solution.

@Asish: Please read all the replies before you decide how to continue. If this is simply a problem about visibility, then Partheban is technically correct; but making everything global (or even public) is generally not the way to write good programs.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!