473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Static Members in a Class/Interface

dmjpro
2,476 2GB
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?

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     int a = 100;
  3. }
  4.  
  5. interface InterfaceTest{
  6.     int a = 100;
  7. }
  8.  
  9. class SubTest extends Test implements InterfaceTest{
  10.     SubTest(){
  11.         System.out.println("A: " + a); //Wrong one (ambiguous reference)
  12.         System.out.println("A: " + InterfaceTest.a); //Right one
  13.     }
  14. }
  15.  
May 21 '09 #1
14 3544
r035198x
13,262 8TB
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.
May 21 '09 #2
dmjpro
2,476 2GB
@r035198x
This is what you telling about coding convention.
My question was ..which one was cost-effective?
May 21 '09 #3
JosAH
11,448 Expert 8TB
@dmjpro
I think you meant to write your example as:

Expand|Select|Wrap|Line Numbers
  1. public class Test { 
  2.     static int a = 100; 
  3.  
... 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:

Expand|Select|Wrap|Line Numbers
  1. class B {
  2.     public static int a= 42;
  3. }
  4.  
  5. class D extends B {
  6.     public static int a= 54;
  7. }
  8.  
  9. public class Test {
  10.     public static void main(String[] args) {
  11.  
  12.         B x= new D();
  13.  
  14.         System.out.println(x.a);
  15.     }
  16. }
  17.  
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
May 21 '09 #4
dmjpro
2,476 2GB
@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.
May 21 '09 #5
Markus
6,050 Expert 4TB
Sorry to jump in.

Jos, can you explain to me why your last code-snippet would return 42.

Thanks.
May 22 '09 #6
JosAH
11,448 Expert 8TB
@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
May 22 '09 #7
Markus
6,050 Expert 4TB
@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)?
May 22 '09 #8
JosAH
11,448 Expert 8TB
@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
May 22 '09 #9
Markus
6,050 Expert 4TB
@JosAH
Data members are overridden, but member methods aren't. Understood.

Thanks, Jos.
May 22 '09 #10
JosAH
11,448 Expert 8TB
@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
May 22 '09 #11
dmjpro
2,476 2GB
@dmjpro
Here is an another example ;)

Expand|Select|Wrap|Line Numbers
  1. public class Test {
  2.     public static void main(String a[]){
  3.         Derived d = new Derived();
  4.     }
  5. }
  6.  
  7. class Super{
  8.     static int a = 100;
  9. }
  10.  
  11. class Derived extends Super{
  12.     static int a = 200;
  13.     Derived(){
  14.         System.out.println(super.a);
  15.     }
  16. }
  17.  
So confusing ..but i have a warning "accessing static field".
May 25 '09 #12
dmjpro
2,476 2GB
@Markus
One more thing static members(only methods, as said my Josh ;) ) don't get overridden.
May 25 '09 #13
dmjpro
2,476 2GB
@JosAH
Have a look at this example @Markus

Expand|Select|Wrap|Line Numbers
  1. class Super{
  2.     int b = 100;
  3. }
  4.  
  5. class Derived extends Super{
  6.     int b = 200;
  7. }
  8.  
  9. //Main Program.
  10. public static void main(String a[]){
  11.         Super d = new Derived();
  12.         System.out.println("The value of b: " + d.b);
  13.     }
  14.  
May 26 '09 #14
JosAH
11,448 Expert 8TB
@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
May 26 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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:...
13
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. ...
7
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.
8
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...
3
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...
17
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...
5
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?
8
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: ...
3
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.