public static void main(String args)
Ar a=new Ar;
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.
Make j a global variable and then you can access it.
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.
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.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.
But giving it public access makes it as good as global.
Tim Cooke wrote: . . . I have not made the j variable global. . . .
Nobody should ever use public fields, except for global constants.
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.
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.