• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Member Variables

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can any one tell me , Does interface has member variables. If they, then do all these member variables need to be STATIC AND FINAL. Why is it so? and do these member variables are public(Strictly) or any access modifier will do.

Also, does abstract classes has member variables. DOes all the member variables of abstract class need to be STATIC AND FINAL. Why is it so? and what could be the access modifier of these member variables of abstract class.

Thanks in advance.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

1. An interface can have member variables. All member variables of an interface are public static and final by default.They cant have any other access modifier.
I think they are static because you cannot instantiate an interface and hence cant have 'object' variables. and so, since they cant be object variables, the only way to use them is to declare them as static.

2. There is no difference in members variables of a 'concrete' class and member variables of an abstract class. so they can be private and need not be static.
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neeraj,

But what is the use of declaring member variables in Interface. Implementing class can not have acces to those variables and also interface methods can not use these member variables.
Then what is the use of having member variables in an Interface.


Thanks in advance
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nilesh Srivastava:
Hi Neeraj,

But what is the use of declaring member variables in Interface. Implementing class can not have acces to those variables and also interface methods can not use these member variables.
Then what is the use of having member variables in an Interface.


Thanks in advance


Yes they can. Try it.

You might do this to define a constant available to all classes (whether they implement the interface or not).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic