File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I use a static method from another class?

 
Angie Mich
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kuyni Kumar
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/**
* 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
Posts: 632
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Angie Mich
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod,

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

Thanks,
Angie
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic