• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

class implementation

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends

following is my question , i am getting identifier expected error. please let me know the mistake in this program.

public interface xyz{

int a = 10;

}

public class abc implements xyz{

int b=5;
System.out.println(a+b);

}
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rajareddy dodda:
hi friends


public interface xyz{

int a = 10;

}

public class abc implements xyz{

int b=5;
System.out.println(a+b);

}



If you look ate carefully , this an executable statement so this must be in a method , rigth.

Hope you found your mistake !
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public interface xyz{

int a = 10;

}


Be aware that it is actually public static final int a. No fields other than public constants are allowed in interfaces. Even though the compiler will add those modifiers for you, it is a good practice to put them in code explicitly. Also, Java code conventions recommend to write constant fields names in uppercase, so A would be better then a





You try to put an execution statement ('System.out.println(a+b);') within a class body. It is not allowed, you have to define a method and put this statement into it, like this:


The statement'int b=5;', on the other hand, will compile, because it is so called initialization statement. It will be executed each time a new instance of your class abc is created (by the way - according to code conventions it would rather be Abc; I know that's just a simple example and I'm being overly scrupulous, but I found out that the more you stick to good practices - even in the simplest examples - the more you see them in your real-life code )


Ooops, Sagar responded quicker... and I gave away the secret :roll:
[ August 07, 2008: Message edited by: Dariusz Kordonski ]
 
rajareddy dodda
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot, i understand my mistake.
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic