• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with two source files under the same package

 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two packages p1 and p2 The code for p1 is given below



The code for package p2 is given below



When i try to compile child_class.java it says that the variable x cannot be found and the class inherit cannot be found. I have never used more than a single source file for compilation till now(Just got the certification book and have started reading it). I searched online and got results which had to do something to do with classpath. OR is it my mistake? Help...
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
achuthan mukundarajan wrote:OR is it my mistake?

Fraid so. If you want to see 'x' in your 'child' class, then it needs to extend 'inherit'.

Winston
 
Junilu Lacar
Bartender
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the code you posted shows child_class declared as being in package p1, not p2. Also, fully qualified package and class names are expected to match the file system structure and locations of source files in folders.

That is, if you have p1 and p2 which you want to have the classes inherit and child_class, respectively, the folder structure and sources files should look something like this:

../p1/inherit.java
../p2/child_class.java

where .. is the path of the folder from which you compile your code.


Classes in the same package can reference each other without an import. You only need an import if the class being referenced is in a different package. Finally, as it is now, child_class cannot reference x because child_class does not extend the inherit class.

One last note: you should try to follow common Java naming conventions
 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i made the following changes

Both classes are in package p1(i.e both inherit.java and child_class.java files are under the same p1 folder )

The code for inherit.java is :



The code for child_class.java is :



Still there are two errors in child_class.java.

1) cannot find class inherit
2)cannot find variable x

??

 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out following:

javac -d . inherit.java

javac -d . child_class.java
 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i now have the error main class not found eventhough i added a main method in child_class.java.

i have tried the -d way. But still no result.

Actually it compiles fine but does not run.(main not found exception)
 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any replies?
 
Junilu Lacar
Bartender
Posts: 7606
54
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "main" that the error is referring to is the public static void main method. Do you see one in your code?
 
achuthan mukundarajan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it"

inherit.java:



child_class.java

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic