• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

make the method in one java file available for a java program in another directory

 
albert kao
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to make the method in one java file available for a java program in another directory?
e.g.
How to make the getTestKey method vailable for a java program in another directory?
GetTestKey.java



KeyClient.java



There are two compile errors:
Only a type can be imported. com.myutil resolves to a package KeyClient.java /XX/src/com/myutil/taglib line 9 Java Problem

The method getTestKey(String) is undefined for the type KeyClient KeyClient.java /XX/src/com/myutil/taglib line 80 Java Problem
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
albert kao wrote:Only a type can be imported. com.myutil resolves to a package KeyClient.java /XX/src/com/myutil/taglib line 9 Java Problem

The error message could be more clear, true. With imports you must either specify a class (e.g. java.util.List) or use a wildcard to import everything from the package (e.g. java.util.*). The former is preferred as it also avoids ambiguity (like java.awt.List and java.util.List when importing java.awt.* and java.util.*).

The method getTestKey(String) is undefined for the type KeyClient KeyClient.java /XX/src/com/myutil/taglib line 80 Java Problem

The getTextKey method is a static method of class GetTestKey. You are calling it directly so the compiler looks for the method in the current class (KeyClient). For instance methods you must use an instance to call a method (instance.instanceMethod(params)); for static methods you should use the class name (MyClass.staticMethod(params)).
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Albert,

You will need to import the class & import statement shoud include the class name or .* after the package name.

You need to learn how to create instances of a class & execute the method.(using the "new operator")

I believe you are jumping from a procedural language to object oriented one so it would help
if you start reading the books mentioned on this forum.

Get a hang of OO concepts & Java basics. Head First Java is a good book to start with.

Keep on posting !!!
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avishkar Nikale wrote:
You need to learn how to create instances of a class & execute the method.(using the "new operator")


Since his method he wants to call is static, he doesn't need an object in this context.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
Avishkar Nikale wrote:
You need to learn how to create instances of a class & execute the method.(using the "new operator")


Since his method he wants to call is static, he doesn't need an object in this context.


Oops !!! My Bad !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic