Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

variables in interface

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


The follwoing gives an error
identifier expected. //1

That means the class can't see the variable "value1" in the interface.
But if i do


It gives the following error.

cannot assign a value to final variable value1 //2

Which is understandable because variables in interface are implicitly final.

What i want to ask is why doesn't it give the same error in 1 i.e why can't it see that a variable "value1" is declared in the interface in the first code snippet.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem in scenario 1 isn't one of visibility, but of syntax. It looks to the compiler that you are declaring a member variable of type value1 and you want to assign a value of 1 to it, but you didn't specify an identifier.

If you provide a type for value1, as in

then you've declared a valid member variable, but hidden the value1 from interface A.

As you've already seen, you can reference value1 in the class, but you can't modify the value. So, you can have

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


when i read from kathy and bert(java cert)
it is said that if you declare a variable in an interface(like you did in your interface declaration), it is implicitly "public static final".
e.g if you declare like:
int i=0; // (1) in an interface it is equal to :
public static final int i=0; // (2)

Therefore in your code, you are trying to change the value of the variable that is declare as final even if you don;t write "final" but it is implicitly "final"(we can't change the value of the primitive if it is declare as final).

sometimes we can;t figure it out if we got an error, but if you know the rules as explained in kathy book, it is very clear why we got an error.

Kathy and bert is a good book, just explored the book, and enjoyed it.


hope it help
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A variable in a interface is static and final you are trying to assign a value to the variable hence the compiler complains
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic