calling of function of one class using objects of another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkmension
    New Member
    • May 2008
    • 1

    calling of function of one class using objects of another

    first of all heres the code

    // first file//
    public class bottom
    {
    public static void main(String args[])
    {
    top obj=new top();
    obj.display();
    }
    }


    //second file//

    public class top
    {
    public void display()

    {
    System.out.prin tln("finaly i got the answer !");
    }

    }

    both are saved in the same folder and i have double checked that the file name are same as that of class name.

    now whenever i try to compile bottom i get the following error

    bottom.java:7: cannot find symbol
    symbol : class top
    location: class bottom
    top obj=new top();
    ^
    bottom.java:7: cannot find symbol
    symbol : class top
    location: class bottom
    top obj=new top();
    ^
    2 errors

    line numbers are not same, but anyways you would get it.

    i dont know why it isn't able to detect top in the same folder.

    please help
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Classes are usually defined to be in packages. When you don't do this, the package defaults to the current directory. Then you need to set the classpath for javac.exe and java.exe:

    Code:
    javac -classpath . bottom.java
    java -classpath . bottom
    Note the dot in those commands (dot = current directory)

    Comment

    Working...