• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static function

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had give the code for my confution bellow.. I am trying to acess a instatnce veriable inside my static function main(). I herd that it is not possible to acess a nonsatic member inside a static function. When i run this code am not geting any kind of error and its sucessfully running with an output 10..How it is possible. My java version is 1.5

Please help me to get out for this cnfton.

class a
{
int i=10;
public static void main(String ar[])
{
a obj =new a();
System.out.println(a);
}

}
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That won't compile. Please check that the code you post is the code you're compiling. Otherwise, everyone wastes their time.
[ October 17, 2007: Message edited by: Peter Chase ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When i run this code am not geting any kind of error and its sucessfully running with an output 10..


I do not know how you are getting 10 !
Here is part of your code


You are only printing a. so how you getting 10?
The code will compile, but it will not print 10.
[ October 17, 2007: Message edited by: Sheikh Sadiruddin ]
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sheikh Sadiruddin:
The code will compile



It does not compile. See the screen capture below. I'm a bit annoyed that you've wasted my time by claiming it compiles, when obviously you haven't tried.



So the point remains: original poster, if you want help, post the code you're actually using, 'cos the code you posted does not do what you say it does.
[ October 17, 2007: Message edited by: Peter Chase ]
 
biny panackal
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry sir,
this is the actual code,
now...why thre is no problem to access i; which i am changing here.
output is 10,25,10

class b
{
int i=10;
public static void main(String arg[])
{
int i=20;
b obj=new b();
System.out.println(obj.i);
b obj2 =new b();
obj2.i=25;
System.out.println(obj2.i);
System.out.println(obj.i);

}

}
}
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by biny panackal:
which i am changing here.



All your accesses of "i" are accessing the instance field, via a reference to an object of class "b". Therefore, they are all legal.

Your method-local variable "i" hides the instance field "i". So, if your code in main() accessed "i" without reference to an object of class "b", it would be a legal, too.

If you didn't hide the field "i" with a method-local variable, then you tried to access "i" without reference to an object of class "b", it would be a compile error.

Because this stuff can confuse the reader, you should aim never to hide a field with a method-local variable. It is legal, but bad practice.
[ October 17, 2007: Message edited by: Peter Chase ]
reply
    Bookmark Topic Watch Topic
  • New Topic