The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
March 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Duplicate filed names are not allowed for a class

Posted by Kishori Sharan on March 25, 2001 at 1:33 PM

You cannot define two class fields with the same name. It doesn't matter whether they are static or non-static or combination of static and non-static. In case of static fields the memory is allocated when the class is loaded for the first time whereas for non-static the memory is allocated when an object is created of that class. If you declare fileds like this
int a=10;
static int a=10;

then for stat class you can refer to static "a" as stat.a as well as using any object like
stat s = new stat();
s.a ;
Now s.a may refer to instance variable "a" as well as static variable "a". So now java will get confused which "a" s.a is referring to. That is why duplicate field names is not allowed in a class.

Thanx
Kishori




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us