What would be the better, whether should i access the static members using class name or using Object? Which one is preferred(low-cost effective)?
Today a thing stroke into my mind... Java supports multiple Interface Implementation but only single class extension to avoid ambiguous reference(data/members). Java manages to avoid method ambiguity but can't data ambiguity ;) Then the question stroke my mind static members should use using class name. But i am not sure which one is low-cost effective? -
public class Test {
-
int a = 100;
-
}
-
-
interface InterfaceTest{
-
int a = 100;
-
}
-
-
class SubTest extends Test implements InterfaceTest{
-
SubTest(){
-
System.out.println("A: " + a); //Wrong one (ambiguous reference)
-
System.out.println("A: " + InterfaceTest.a); //Right one
-
}
-
}
-
14 3544
Use the class name always. Anyone looking at the code will be able to tell it's a static just by how you called it.
@r035198x
This is what you telling about coding convention.
My question was ..which one was cost-effective?
@dmjpro
I think you meant to write your example as: -
public class Test {
-
static int a = 100;
-
}
-
... and as said before always write Test.a, not o.a where o is an object of class Test. Also it makes more clear which static member you want. Compare the following (obfuscated) code snippet: -
class B {
-
public static int a= 42;
-
}
-
-
class D extends B {
-
public static int a= 54;
-
}
-
-
public class Test {
-
public static void main(String[] args) {
-
-
B x= new D();
-
-
System.out.println(x.a);
-
}
-
}
-
What will be printed? 42 or 54? If you had written B.a or D.a it would've been clear immediately. There are no performance issues, better spend your energy on useful stuff (check it out with javap).
kind regards,
Jos
@JosAH
Obviously the print will be 42. So you are telling that there is no performance issue..right! For coding convention it's better to use class name.
Sorry to jump in.
Jos, can you explain to me why your last code-snippet would return 42.
Thanks.
@Markus
Local variable x is of type B (I declared it that way so that's all the compiler knows) so x.a is B's static member variable a which is 42.
kind regards,
Jos
@JosAH
So, if you create a new object of x, where x extends y, with the type of y, the new object will take on the members/properties of it's type ( y) , not the class it created an object from ( x)?
@Markus
Yep, and the same counts for non-static members; remove the 'static' keyword in both classes in my example and you get 42 as output as well. The compiler decides which variable to use and from the 'outside world' the compiler knows no better than that x is a B; data members don't override they can only be hidden in the 'inside world' i.e. in code inside a method of a class.
Not so with method members: they are overridden ('virtual' functions in C++). That's why public data members are a no-no in object oriented programming. But, if the design of the language would've been such that data members were also overridden, the vft (Virtual Function Table) needs to contain pointers to all data members of (an object of) a class as well.
kind regards,
Jos
@JosAH
Data members are overridden, but member methods aren't. Understood.
Thanks, Jos.
@Markus
Nonono, the terminology is: data members can be hidden and method members can be overridden; the first is a compiler issue, the second one is handled during runtime.
kind regards,
Jos
@dmjpro
Here is an another example ;) -
public class Test {
-
public static void main(String a[]){
-
Derived d = new Derived();
-
}
-
}
-
-
class Super{
-
static int a = 100;
-
}
-
-
class Derived extends Super{
-
static int a = 200;
-
Derived(){
-
System.out.println(super.a);
-
}
-
}
-
So confusing ..but i have a warning "accessing static field".
@Markus
One more thing static members(only methods, as said my Josh ;) ) don't get overridden.
@JosAH
Have a look at this example @ Markus -
class Super{
-
int b = 100;
-
}
-
-
class Derived extends Super{
-
int b = 200;
-
}
-
-
//Main Program.
-
public static void main(String a[]){
-
Super d = new Derived();
-
System.out.println("The value of b: " + d.b);
-
}
-
@dmjpro
Don't make this an obfuscated thread; we were discussing static members. Don't throw in a completely other example or add a bit of explanatory text or a clear question.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Scott J. McCaughrin |
last post by:
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member:...
|
by: Adam H. Peterson |
last post by:
I just made an observation and I wondered if it's generally known (or
if I'm missing something). My observation is that static protected
members are essentially useless, only a hint to the user. ...
|
by: mdc |
last post by:
Hi,
Is there any way to implement an interface as a static method in C#? If
not, is this a bug?
Micheal.
|
by: Steven Livingstone |
last post by:
Anyone able to explain to me why you cannot define an interface that can
then be implemented using static methods?
I understand the C# CLS states this, but just interested in the reasons
behind...
|
by: Erik Harris |
last post by:
I apologize if this is a stupid question - I'm relatively new to OOP.
I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
|
by: Picho |
last post by:
Hi all,
I popped up this question a while ago, and I thought it was worth checking
again now... (maybe something has changed or something will change).
I read this book about component...
|
by: Dale |
last post by:
Is it possible to declare a method of an interface as static?
For instance, can I create an interface that defines a static method and 2
instance methods?
|
by: dtarczynski |
last post by:
Hello all.
I have to implement IEnumerator interface in my (static) class. But
compilers throws me an error:
'GetEnumerator': cannot declare instance members in a static class
For example:
...
|
by: =?Utf-8?B?TkVXMi5ORVQ=?= |
last post by:
I have a static event declared in a C++ ref class, that can then be handled
in a VB app. I'm trying to expose the static event through the interface
that the C++ ref class implements so the VB app...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |