Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about modifier

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
anu kuntala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 !
 
sumi rankan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thank you all ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic