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

Doubt about modifier

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class inheritex
{
int i=5;
}
class inheritex1 extends inheritex
{
int i=3;
static void main(String args[])
{
inheritex1 ex=new inheritex1();
System.out.println(ex.i);
}
}

Here i am getting the error, class inheritex is public and should be declared in a file named inheritex.java
what's wrong in this?
please explain me this....

Thanks
Preparing scjp5
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry here is the correct code!


public class inheritex
{
int i=5;
}
class inheritex1 extends inheritex
{
int i=3;
public static void main(String args[])
{
inheritex1 ex=new inheritex1();
System.out.println(ex.i);
}
}

Here i am getting the error, class inheritex is public and should be declared in a file name inheritex.java
what's wrong in this?
please explain me this....

Thanks
Preparing scjp5
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Preetha,

You have not mentioned your file name. What have you given as file name.
check for spelling mistakes.
 
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 Preetha Arun:

Here i am getting the error, class inheritex is public and should be declared in a file name inheritex.java



If you have a Class definition whose access specifier is 'public' , then you must save the file with public class name !

Like In your example , your file name is "inheritex.java" !

For further information , follow this link !
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class x is declared public it should be saved in the file x.java.otherwise the compiler complains.This restriction is not enforced if both the classes have default access.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic