wood burning stoves*
The moose likes Beginning Java and the fly likes working with two source files under the same package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working with two source files under the same package" Watch "working with two source files under the same package" New topic
Author

working with two source files under the same package

achuthan mukundarajan
Ranch Hand

Joined: May 30, 2012
Posts: 87
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

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


Junilu - [How to Ask Questions] [How to Answer Questions]
achuthan mukundarajan
Ranch Hand

Joined: May 30, 2012
Posts: 87
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

Joined: Jun 02, 2012
Posts: 187
Try out following:

javac -d . inherit.java

javac -d . child_class.java
achuthan mukundarajan
Ranch Hand

Joined: May 30, 2012
Posts: 87
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

Joined: May 30, 2012
Posts: 87
Any replies?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

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

Joined: May 30, 2012
Posts: 87
I fixed it"

inherit.java:



child_class.java

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with two source files under the same package