Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

classes/packages..

0 views
Skip to first unread message

maya

unread,
Feb 9, 2009, 12:41:35 PM2/9/09
to

I don't know why all of a sudden am having problems w/this... a class
can easily access variables in other classes in the same package, right????

main class:
----------
package test;
class helloworld {
public static void main (String args[]) {
System.out.println(helper.hh);
}
}


helper class:
------------
package test;
class helper {
String hh = "yada yada";
}

helper.java compiles fine, but when I try to compile helloworld.java I
get this error:

helloworld.java:9: cannot find symbol
symbol : variable helper
location: class test.helloworld
System.out.println(helper.hh);


this is something I learned years ago, when I first started learning
java (about five years ago); I don't understand why I'm having problems
with this simple stuff now...

thank you very much..

Daniel Pitts

unread,
Feb 9, 2009, 1:05:31 PM2/9/09
to

The compiler says it can't find the helper class. To simplify things,
make sure helloworld.java and helper.java are in the same directory.

Please note: It is strongly preferable convension to use UpperCamalCase
for class names (eg, HelloWorld and Helper).

Also, most of the time you should avoid "package-private" data member.
There may be some case where it is useful/necessary, but it leads to
unnecessary coupling and inflexible design.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Eric Sosman

unread,
Feb 9, 2009, 1:23:25 PM2/9/09
to

"hh" is an instance variable, an element that is part of
every "helper" instance. You cannot reference "hh" as an
element of the "helper" class, but only as an element of a
particular "helper" object. So the main() method in "helloworld"
would look something like

public static void main(String[] args) {
helper aide = new helper();
System.out.println(aide.hh);
}

Alternatively, you could leave main() the way it is but
change "hh" so it is a class variable instead of an instance
variable:

static String hh = "yada yada";

By the way, you will avoid a lot of frustration if you get
in the habit of using the same typographical conventions everyone
else does. Name your classes "HelloWorld" and "Helper," using
an upper-case letter to begin each word of each name.

--
Eric....@sun.com

maya

unread,
Feb 9, 2009, 1:21:01 PM2/9/09
to

thank you for your response.. of course both classes are in dir
'test'... (sorry, forgot to mention this in OP)

I know about class-naming conventions, this is just a test....;)

not sure what you mean by yr third point, regarding "package-private"
data member.. I added "public" to var in helper class..

package test;
class helper {
public String hh = "yada yada";
}

same result.. oh brother.. this IS weird...

thank you...

Lew

unread,
Feb 9, 2009, 1:33:38 PM2/9/09
to
Daniel Pitts wrote:
>> The compiler says it can't find the helper class. To simplify things,
>> make sure helloworld.java and helper.java are in the same directory.
>

This is not strictly necessary. They only need to be in the same-
named directory relative to some classpath entry, not necessarily in
the same absolute directory. (And not even that if you use a non-
directory-based classpath.)

The real advice, that the OP already followed, is to have the classes
in the same package. It had nothing to do with their problem. Eric
Sosman identified the problem.

>> Please note: It is strongly preferable convension to use UpperCamalCase
>> for class names (eg, HelloWorld and Helper).
>

>> Also, most of the time you should avoid "package-private" data member[s].

There are frequently good reasons to use package-private access.

>> There may be some case where it is useful/necessary, but it leads to
>> unnecessary coupling and inflexible design.
>

Not necessarily, and not too terribly inflexible. In fact, quite
often it makes a convenient alternative to private access.

> thank you for your response..  of course both classes are in dir
> 'test'... (sorry, forgot to mention this in OP)
>

Again, not related to your problem.

> I know about class-naming conventions, this is just a test....;)
>

Not a good enough reason to violate the convention.

> not sure what you mean by yr third point, regarding "package-private"
> data member..

"Package-private" is a synonym for "default" access, i.e., no access
specifier provided. It means that only the declaring class or classes
in the same package have access.

> I added "public" to var in helper class..

Yikes! Don't do that!

>       package test;
>       class helper {
>         public String hh = "yada yada";
>      }
>
> same result..  oh brother.. this IS weird...

Read Eric Sosman's answer. It's not at all weird that an instance
variable is inaccessible without an instance.

--
Lew

Joshua Cranmer

unread,
Feb 9, 2009, 1:32:04 PM2/9/09
to
maya wrote:
> helper.java compiles fine, but when I try to compile helloworld.java I
> get this error:

did you try |javac *.java| or just |javac helloworld.java| ?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Lew

unread,
Feb 9, 2009, 1:36:50 PM2/9/09
to
maya wrote:
>>     helloworld.java:9: cannot find symbol
>>     symbol  : variable helper
>>     location: class test.helloworld
>>         System.out.println(helper.hh);
>

Daniel Pitts wrote:
> The compiler says it can't find the helper class.
>

Actually, the message said that it couldn't find the 'helper'
*variable*.

--
Lew

maya

unread,
Feb 9, 2009, 1:37:19 PM2/9/09
to

thank you..

package test;
class Helper {
public String hh = "yada yada";
}


package test;
class Helloworld {


public static void main (String args[]) {

Helper aide = new Helper();
System.out.println(aide.hh);
}
}


same result!!!

Helloworld.java:6: cannot find symbol
symbol : class Helper
location: class test.Helloworld
Helper aide = new Helper();

> Alternatively, you could leave main() the way it is but
> change "hh" so it is a class variable instead of an instance
> variable:
>
> static String hh = "yada yada";

same result after putting "static" next to var in Helper class...

package test;
class Helper {
public static String hh = "yada yada";
}

package test;
class Helloworld {


public static void main (String args[]) {

// Helper aide = new Helper();
// System.out.println(aide.hh);
System.out.println(Helper.hh);
}
}


Helloworld.java:8: cannot find symbol
symbol : variable Helper
location: class test.Helloworld
System.out.println(Helper.hh);

?????????????????????

I REALLY don't get this...... :(

thanks again...

Mark Space

unread,
Feb 9, 2009, 1:49:31 PM2/9/09
to
maya wrote:

> Helloworld.java:6: cannot find symbol
> symbol : class Helper
> location: class test.Helloworld
> Helper aide = new Helper();

Joshua got it, I think. Make sure you have compiled the class Helper
first. Javac won't look at source files unless they're on the command
line, so if you just do "javac Helloworld.java" then if there's no
Helper.class file, it won't compile.

An IDE (like NetBeans) or a build management tool (like Ant) would help
you immensely.

Lew

unread,
Feb 9, 2009, 1:52:19 PM2/9/09
to
maya wrote:
> same result!!!
>
> Helloworld.java:6: cannot find symbol
> symbol  : class Helper
> location: class test.Helloworld
>            Helper aide = new Helper();
>

I tried to duplicate your result. Here's what I got, substituting
package 'eegee' for 'test':

//---------------
package eegee;


class Helper
{
public String hh = "yada yada";
}

//---------------
package eegee;
class Helloworld
{
public static void main ( String args [] ) {


Helper aide = new Helper();
System.out.println( aide.hh );
}
}

//---------------

$ javac -d build/classes/ src/eegee/Helloworld.java
src\eegee\Helloworld.java:5: cannot find symbol
symbol : class Helper
location: class eegee.Helloworld


Helper aide = new Helper();

^
src\eegee\Helloworld.java:5: cannot find symbol
symbol : class Helper
location: class eegee.Helloworld


Helper aide = new Helper();

^
2 errors
//---------------

This tells me that I haven't compiled 'Helper.java' yet. One more
try:
//---------------
$ javac -d build/classes/ src/eegee/Helloworld.java src/eegee/
Helper.java

$ java -cp build/classes/ eegee.Helloworld
yada yada

//---------------

Success!

You seem to have forgotten to compile Helper.java.

--
Lew

maya

unread,
Feb 9, 2009, 2:24:39 PM2/9/09
to
Joshua Cranmer wrote:
> maya wrote:
>> helper.java compiles fine, but when I try to compile helloworld.java I
>> get this error:
>
> did you try |javac *.java| or just |javac helloworld.java| ?
>

oh my gosh, this worked.. I thought helper (meant in a generic sense)
class had to compiled first (that if helper class is not compiled first
main class (compiler?) wouldn't be able to find vars in helper class..)
so now both classes compile fine, however:

when try to run Helloworld class get following error:

Exception in thread "main" java.lang.NoClassDefFoundError: Helloworld
(wrong name: test/Helloworld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
etc......


oh brother..

once again both classes are in dir 'test' -- and run javac and java
commands from inside that dir...

thank you very much to all for your help...

Lew

unread,
Feb 9, 2009, 3:26:49 PM2/9/09
to
On Feb 9, 2:24 pm, maya <maya778...@yahoo.com> wrote:
> when [I] try to run Helloworld class [I] get [the] following error:

>
> Exception in thread "main" java.lang.NoClassDefFoundError: Helloworld
> (wrong name: test/Helloworld)
>          at java.lang.ClassLoader.defineClass1(Native Method)
>          at java.lang.ClassLoader.defineClass(Unknown Source)
>                   etc......
>
> oh brother..
>
> once again both classes are in dir 'test' -- and run javac and java
> commands from inside that dir...

The default classpath is ".", the current directory. Packages are
relative to that. Thus, the JVM is looking for "test.Helloworld"
relative to "test/", i.e., "test/test/Helloworld.class".

You should run 'java' and 'javac' from a directory *above* 'test/',
and use the "-classpath" (also known as the "-cp") option to 'java' to
specify the directory immediately above 'test/'.

The example I posted upthread with package 'eegee' shows how I invoked
'javac' and 'java' from the project directory 'eegee/', which has the
structure:

eegee/
|___src/eegee/Helloworld.java
|
|___build/classes/eegee/Helloworld.class

The tutorials will help you:
<http://java.sun.com/docs/books/tutorial/java/package/
managingfiles.html>
etc.

as will the tools documentation:
<http://java.sun.com/javase/6/docs/technotes/tools/index.html>
particularly
<http://java.sun.com/javase/6/docs/technotes/tools/solaris/
classpath.html>
<http://java.sun.com/javase/6/docs/technotes/tools/windows/
classpath.html>

You will not get far with Java until you comprehend the classpath.

--
Lew

Lew

unread,
Feb 9, 2009, 3:33:13 PM2/9/09
to
On Feb 9, 2:24 pm, maya <maya778...@yahoo.com> wrote:
> Joshua Cranmer wrote:
> > maya wrote:
> >> helper.java compiles fine, but when I try to compile helloworld.java I
> >> get this error:
>
> > did you try |javac *.java| or just |javac helloworld.java| ?
>
> oh my gosh, this worked..  I thought helper (meant in a generic sense)
> class had to compiled first (that if helper class is not compiled first
> main class (compiler?) wouldn't be able to find vars in helper class..)
>   so now both classes compile fine, however:

It doesn't have to be compiled first, but it does have to be
compiled. Your compiler error said that Helper.class was never
compiled. This is likely because you had the wrong classpath
specified for the 'javac' command.

Upon review of your posts, I note that you never showed us the 'javac'
command you used, nor ever told us the directory from which you
invoked it. These are relevant data that you should not have omitted,
as they bear directly on your difficulties. It is counterproductive
to ask for help and provide incomplete information.

--
Lew


--
Lew

maya

unread,
Feb 9, 2009, 3:52:29 PM2/9/09
to

thank you very much Lew.. I do comprehend the classpath, I read up on
it just last night -- for the nth time -- in O'Reilly's Learning Java
(3rd ed, pg 70)

will study what you said though.. thank you very much...


Roedy Green

unread,
Feb 9, 2009, 10:20:15 PM2/9/09
to
On Mon, 09 Feb 2009 12:41:35 -0500, maya <maya7...@yahoo.com> wrote,
quoted or indirectly quoted someone who said :

>I don't know why all of a sudden am having problems w/this... a class
>can easily access variables in other classes in the same package, right????

if they have the right scope. See
http://mindprod.com/jgloss/scope.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer

0 new messages