Creating a Library

B

bruce

How do I create a library of common methods without instantiating a
class which gives me access to the method? I know how to create a
library that has a class and the method. I can then:

CommonCode cc = new CommonCode();
String result = cc.myMethod();

This works fine but what I want to do is have the library contains
methods so I don't have to instantiate the class: I want to just do:

String result = myMethod();

Again, I know I can place it in my project but I want the library
available for many projects.

Thanks..

Bruce
 
K

Knute Johnson

How do I create a library of common methods without instantiating a
class which gives me access to the method? I know how to create a
library that has a class and the method. I can then:

CommonCode cc = new CommonCode();
String result = cc.myMethod();

This works fine but what I want to do is have the library contains
methods so I don't have to instantiate the class: I want to just do:

String result = myMethod();

Again, I know I can place it in my project but I want the library
available for many projects.

Thanks..

Bruce

static

class myUtils {
public static String myMethod() {
return "hello world!";
}
}

System.out.println(myUtils.myMethod());
 
M

markspace

How do I create a library of common methods without instantiating a
class which gives me access to the method? I know how to create a
library that has a class and the method. I can then:


First you need to use something like:

import my.library.CommonCode;

Then you'd do:
> CommonCode cc = new CommonCode();
String result = cc.myMethod();

or:

String result = CommonCode.myMethod();

or:

MyObject o = CommonCode.newMyObject();
String result = o.myMethod();



To do this you:

1. Make a regular old CommonCode.java source file.
2. Compile it.
3. Jar it.

Then add the Jar file to your class path. With javac that's the -cp
parameter. With an ant build script you should have a directory or
other parameter than gets included by default. With most IDE's there's
a click button to "Add Library..." and you just use that.
 
B

bruce

static

class myUtils {
     public static String myMethod() {
         return "hello world!";
     }

}

System.out.println(myUtils.myMethod());

This is close. But I would like to
 
B

bruce

static

class myUtils {
     public static String myMethod() {
         return "hello world!";
     }

}

System.out.println(myUtils.myMethod());

This is close but I'd like to:

System.out.println(myMethod());

That is with not class reference.

Thanks.

Bruce
 
B

bruce

First you need to use something like:

   import my.library.CommonCode;

Then you'd do:

 > CommonCode cc = new CommonCode();


or:

   String result = CommonCode.myMethod();

or:

   MyObject o = CommonCode.newMyObject();
   String result = o.myMethod();

To do this you:

1. Make a regular old CommonCode.java source file.
2. Compile it.
3. Jar it.

Then add the Jar file to your class path.  With javac that's the -cp
parameter.  With an ant build script you should have a directory or
other parameter than gets included by default.  With most IDE's there's
a click button to "Add Library..." and you just use that.

Thanks for the response. I can get my example to work. I am wondering
if I can use "myMethod()" without a class reference from a library.

Bruce
 
M

markspace

Thanks for the response. I can get my example to work. I am wondering
if I can use "myMethod()" without a class reference from a library.



If "myMethod()" is static, you can do a:

import static my.library.CommonCode.myMethod;

and then use it without the CommonCode name.
 
L

Lew

bruce said:
This is close but I'd like to:

System.out.println(myMethod());

That is with not class reference.

There will always be a class reference, but you can make it implicit with
'import static'.
 
L

Lew

Knute said:
static

class myUtils {
public static String myMethod() {
return "hello world!";
}
}

System.out.println(myUtils.myMethod());

Class names should start with an upper-case letter.

OP: The 'System.out' call that Knute showed will itself be invoked from some
other method, not standalone. He's just illustrating that 'static' methods
will do what you asked.

You should read the Java tutorials. Then read more advanced stuff. These
questions are answered in the tutorials, though.

http://download.oracle.com/javase/tutorial/index.html
 
A

Arne Vajhøj

This is close but I'd like to:

System.out.println(myMethod());

That is with not class reference.

Several other have pointed you to the static import.

Which does exactly what you want.

But I will recommend you not to overuse the feature.

It can make the code more difficult to read.

It only makes sense if the the name of the method
is so telling that the class is not necessary.

An example of where that is the case is the Math
method.

Any developer reading a method call cos will have
very clear understanding of what it does even without
knowing that it is in class Math.

Arne
 
L

Lew

Arne said:
Several other have pointed you to the static import.

Which does exactly what you want.

But I will recommend you not to overuse the feature.

It can make the code more difficult to read.

It only makes sense if the the name of the method
is so telling that the class is not necessary.

An example of where that is the case is the Math
method.

Any developer reading a method call cos will have
very clear understanding of what it does even without
knowing that it is in class Math.

Allow me to reiterate that 'import static' does not remove the class
reference. The static method (or other member) still belongs to the class
referenced in the 'import static' declaration; the class reference is just
shorthanded so you don't have to repeat it over and over.
 
R

Roedy Green

Thanks for the response. I can get my example to work. I am wondering
if I can use "myMethod()" without a class reference from a library.

if you have the name of the class as a string, you can use
Class.forName without mentioning the classname in the code. See
http://mindprod.com/jgloss/classforname.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
To err is human, but to really foul things up requires a computer.
~ Farmer's Almanac
It is breathtaking how a misplaced comma in a computer program can
shred megabytes of data in seconds.
 
I

Ian Pilcher

Thanks for the response. I can get my example to work. I am wondering
if I can use "myMethod()" without a class reference from a library.

The short answer is no. All Java methods are associated with a class.
It's the nature of the language.

The "import static" construct will allow you to omit the class name in
your code (except for the import statement itself), but the method is
still associated with a class.
 
A

Arne Vajhøj

Allow me to reiterate that 'import static' does not remove the class
reference. The static method (or other member) still belongs to the
class referenced in the 'import static' declaration; the class reference
is just shorthanded so you don't have to repeat it over and over.

It is removed from the source code.

The byte code is unaffected - it has always full
package and class name.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top