• 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

working with two source files under the same package

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

??

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any replies?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed it"

inherit.java:



child_class.java

 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic