This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Member Variables

 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic