my dog learned polymorphism*
The moose likes Beginning Java and the fly likes static function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static function" Watch "static function" New topic
Author

static function

biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
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);
}

}


Biny Balan
binypanackal@gmail.com
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Sheikh Sadiruddin
Greenhorn

Joined: Aug 10, 2007
Posts: 21

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 ]

SCJP 6 (80%) || SCWCD 5 (92%)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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

Joined: Apr 12, 2007
Posts: 72
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

Joined: Oct 30, 2001
Posts: 1970
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static function