aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Instance variables in interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Instance variables in interface" Watch "Instance variables in interface" New topic
Author

Instance variables in interface

kanaka krishna
Greenhorn

Joined: Oct 31, 2007
Posts: 18
Hi Cameron,
In your book you have said interface cannot have instance variables, can you please tell what you mean by that, as variables can be declared in interfaces, so what is it we cannot declare.
Please explain.

Thanks
kanaka
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

When used effectively, interfaces become extremely powerful plugin points that make your design incredibly flexible. Applied effectively, they can become one of the most important parts of an application design.

Interfaces express a common behavior, and in Java, behavior translates into methods. So, in Java interfaces, we simply declare methods for which implementing classes will provide an implementation. In fact, the methods we declare in an interface are said to be implicitly abstract, although they don't necessarily need to have the abstract keyword - doesn't hurt to put it in though.

But interfaces are flexible because they allow the implementing classes to deliver a code body in any way that makes sense. If a concrete implementing class needs ten instance variables to implement an interface method, it can declare ten instance variables. If a concrete implementing class doesn't need any instance variables to provide a concrete implementation, then it doesn't need to declare any instance variables. In a world where every programmer has their own idea of the best way to implement something, Java interfaces provide implementation flexibility, appealing to the ego of all of those narcissistic programmers out there.

But an interface indeed does not allow you to declare any instance variables. Using a variable declared in an interface as an instance variable will trigger a compile time error. It simply can't be done.

What you CAN do in an interface is declare a constant, or in Java-speak, you can declare a static final variable. But this is different from an instance variable. A static final variable is really just associated once with the JVM that is running, and it is not tied to any particular instance. I do like declaring constants in an interface, but even then, there is something known as the "constants interface ANTI-pattern' which new developers can fall into if they take the practice too far.

If you do wish to declare instance variables in a component that also declares abstract classes, you can declare an abstract class. An abstract class is that bridge between a highly abstract interface, and a concrete class that provides a full implementation.

Of course, I can spout off all day about Java, and Interfaces, and Abstract classes. The final word always comes down to the compiler. I can have all the opinions in the world, but at the end of the day, they must all be in line with what the compiler spits out.

Take a look at the following code. It might just tackle the source of your confusion with regards to Java interfaces and instance variables.

Curiously, the following code will compile :



Looks like an instance variable, doesn't it?

Here's how you'd use it in code:



This will actually run, and print out 10. But notice, the variable is being referenced as though it were a static variable, not an instance variable.

Now look what happens when you try to change the value of i:



Now, the bolded line that tries to change the value of i generates a compile error, saying:

The final field Instanceable.i cannot be assignedInstanceableRunner.javaline 7

So, as you can see, the instance variable is declared using a syntax that makes it look like it is an instance variable, but when you try to reference or change the value, you see that it is in fact a constant, or static final variable. So, just as any method declared in an interface is implicitly abstract, any variable declared in an interface is implicitly a static final variable. The code really should have been written like this:



Looks like I just wrote a few extra pages for the next printing of my SCJA Study Guide.

Thanks for the question, and if there's anything that's not clear, just post. Create a new thread for new topics, but if it applies to this topic, continue the thead.

Kindest regards,

-Cameron McKenzie
[ November 15, 2007: Message edited by: Cameron McKenzie ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

It was an awesome and detailed explanation Cameron McKenzie.

So kind of you.

But the last code snippet should have been like



That's when it makes sense right? The compiler prohibits the value of a variable declared in an interface changed!

Am i correct?

Thank you.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I'm not sure what you're talking about? That is clearly what my post indicates!

Thank goodness for the edit button. See what happens when you write code without a compiler?

I've always said, it's more important to know what I mean, as opposed to what I say. If people just knew what I meant without me having to say anything, the world would be a much better place.

Good catch.

-Cameron
[ November 15, 2007: Message edited by: Cameron McKenzie ]
kanaka krishna
Greenhorn

Joined: Oct 31, 2007
Posts: 18
WOW!! What an explanation...
Its cool to learn this way, now Iam sure I will not blunder variables and interfaces in the exam
Thanks Cameron.

Kanaka
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thank you Cameron. But i thought it would add a feather to your crown.

aint I? Nice of you to have accepted and corrected the same by mixing a comical sense
Ovidiu Mura
Greenhorn

Joined: Jun 18, 2011
Posts: 2

Very nice explanation! Thanks Cameron
Anand Jhunjhunwala
Greenhorn

Joined: Jun 11, 2007
Posts: 3
Superb explanation makes it simpler to remember that instance variables cannot be used in Interface - full stop. If we need we can use Instance Variables in Abstract class and which can inherited by it's subclasses but not in Interface (which is a highly abstract class). Superb explanation Cameron - thanks.


SCJP 1.4
Kapil Aggarwal
Greenhorn

Joined: Dec 09, 2013
Posts: 1
Hi Cameron/ forum members,

Need your help in explaining one more thing. While running the program in 'main' class, the compiler asks for necessarily declaring the variable in 'abstract parent class' or 'interface' even when the same is declared in corresponding extending or implementing classes. Further, it takes the value of the variable initialized in abstract class/interface. If you could please help me resolve the problem. I hope I am not doing something wrong. Many thanks.

Cheers
Kapil


Cameron Wallace McKenzie wrote:I'm not sure what you're talking about? That is clearly what my post indicates!

Thank goodness for the edit button. See what happens when you write code without a compiler?

I've always said, it's more important to know what I mean, as opposed to what I say. If people just knew what I meant without me having to say anything, the world would be a much better place.

Good catch.

-Cameron
[ November 15, 2007: Message edited by: Cameron McKenzie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance variables in interface