Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How do I use a static method from another class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I use a static method from another class?" Watch "How do I use a static method from another class?" New topic
Author

How do I use a static method from another class?

Angie Mich
Greenhorn

Joined: Feb 03, 2011
Posts: 4
I'm new to Java and I'm writing my first program (straight out of the textbook) that uses a class I just created in the same package. My program calls a static method from a simple class I just created. I get the following error message:
TestRandomCharacter.java:11: cannot find symbol
symbol : variable RandomCharacter
location: class Chpt5.TestRandomCharacter
char ch = RandomCharacter.getRandomLowerCaseLetter();

Here's the first class, which compiled fine:


And here's the second class, which I'm trying to compile from the same directory:


I've tried messing around with classpath a little based on some other posts I read, but to no avail. I didn't think that should be a problem though, since the class I'm trying to reference is in the same directory (and package). I'm trying to compile from the command line.

I've considered drop-kicking my computer, but I thought I'd ask for help here first.

Thanks in advance helping!
Angela
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Welcome to the ranch !
I hope you haven't kicked your computer yet You'll find that the problem more often comes from us, than from the computer.

The compiler does not find the symbol RandomCharacter. Which directory are you compiling from ? If you are in the Chpt5 directory, you probably need to put the current directory in your classpath : javac -cp . TestRandomCharacter.java


[My Blog]
All roads lead to JavaRanch
Kuyni Kumar
Greenhorn

Joined: Jan 18, 2011
Posts: 14
/**
* Hi Angie Mich , try this below code with the concept of static import. This will work for you.
*
*/

import static coderanch.RandomCharacter.*;

public class TestRandomCharacter
{
/** Main method */
public static void main(String[] args)
{
final int NUMBER_OF_CHARS = 175;
final int CHARS_PER_LINE = 25;

// Print random characters between 'a' and 'z', 25 per line
for (int i = 0; i < NUMBER_OF_CHARS; i++)
{
char ch = RandomCharacter.getRandomLowerCaseLetter();
if ((i + 1) % CHARS_PER_LINE == 0)
System.out.println(ch);
else
System.out.print(ch);
}
}
}
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hi, Angie Welcome to JR. compile program as :

Suppose your program in D:\Pramod\Basics\Java Program> directory

Then compile it as
D:\Pramod\Basics\Java Program>javac -d . RandomCharacter.java

D:\Pramod\Basics\Java Program>javac -d . TestRandomCharacter.java

and to run program enter command as

D:\Pramod\Basics\Java Program>java Chpt5.TestRandomCharacter


Life is easy because we write the source code.....
Angie Mich
Greenhorn

Joined: Feb 03, 2011
Posts: 4
Thank you Christophe!

I am compiling in the Chpt5 directory, and your suggestion worked (sort of). I successfully compiled by putting the classpath in the command :
javac -cp C:\CITP190 TestRandomCharacter.java

As you have probably guessed, the Chpt5 directory is in C:\CITP190. Using the "." didn't work to reference the current directory. I also tried adding C:\CITP190 to the classpath variable in my environment variables, which didn't work. Here's what I have for my classpath in my environment variables:
.;C:\Program Files (x86)\QuickTime\QTSystem\QTJava.zip
I tried changing it to this:
.;C:\Program Files (x86)\QuickTime\QTSystem\QTJava.zip;C:\CITP190
I'm doing all of the examples in the textbook as I read through it and am making a new package for each chapter, all in the C:\CIPT190 directory. Do you have any suggestions for what I can do so that I don't have to type "-cp C:\CITP190" every time I compile?

Thanks again!
Angie
Angie Mich
Greenhorn

Joined: Feb 03, 2011
Posts: 4
Pramod,

What does the -d do in "javac -d . RandomCharacter.java"?

Thanks,
Angie
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

It will compile your program and placed .class file in directory which is mentioned in your package statement. Here in your program it creates Chpt5 folder and in that folder puts these two class files.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Using the "." didn't work to reference the current

My bad. Your classes are in the "Chpt5" package, so that package has to be in the classpath. If you were in C:\CITP190, the following command would have compiled your source successfully : javac -cp . Chpt5.TestRandomCharacter

Do you have any suggestions for what I can do so that I don't have to type "-cp C:\CITP190" every time

Use the CLASSPATH environment variable, and compile from C:\CITP190 : set CLASSPATH=C:\CITP190;%CLASSPATH%, or set CLASSPATH=.;%CLASSPATH%


Angie Mich
Greenhorn

Joined: Feb 03, 2011
Posts: 4
Ok, I think I have the environment variables set correctly now. I can compile without specifying the classpath. Thanks again for all your help!

Angie
 
 
subject: How do I use a static method from another class?
 
Similar Threads
characters can not be displaied for codes between 0 and 65,535
File Letter Counter help
Swapping of two Strings in the form of character arrays.....
Ordering of AlphaNumeric No. in ascending Order
hashCode