classes in a package talking to each other

Posted on 2000-05-16
Last Modified: 2010-04-01
so say if I have few classes all in
and I am saying
package a.dir.where.classes.are;
at the top of all.
Why can I not call a class from a diffredn
calls in the same package. I mean I have
say to classes in this dir and I want to
call one form the other.
But if I do I get told after it compiles
ok. when I try to run it
Error loading class mytest: Wrong name

also how can I just call the class name
if I only have one function in a class?

I should say it works ok if I put mytest
out side the dir and import the package

Thank you for helping
Question by:thedogeater
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Expert Comment

ID: 2815172
Have you some code we can examine?

Definitly you can access a class in the same package.

If your class mytest has a package statement then the name is not mytest.
The name is package.mytest
In your example
so say if I have few classes all in

What do you mean by calling a class?

Second, you can not call only the class if there is only one method defined in the class.
At least ther will be more methods, because all classes extending Object, so
there are a lot of methods in you class.
You can define a method static and then call it with

Expert Comment

ID: 2815343
This probably isn't the answer, but I need a reminder of this from time to time. make sure that you have the exact same package name, I frequently will put


when I mean



Author Comment

ID: 2815732
well say I have this

package my.package;

public class atest{
  public static void main(String args[]){
  String s = "why not is working";

and in a new file in same dir

package my.package

public class trying{
  public static void tocall (String meg){

kind of thing what is it I am doing wrong?
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


Author Comment

ID: 2815737
alos how can I say have a class where I
will put a load of values which I will
use in all classes do I have to make
the all public of is this a easy way?
to ask more:+)


Author Comment

ID: 2815746
alos how can I say have a class where I
will put a load of values which I will
use in all classes do I have to make
the all public of is this a easy way?
to ask more:+)


Expert Comment

ID: 2816141
I have tried your example and it works witk little modifications:
- my.package; -> my.test;
- putting the files in a directory my/test
- compiling javac my/test/
- compiling javac my/test/

java my.test.atest
why not is working


Author Comment

ID: 2822719
:+) it works thanks falter. I am happy
post for the points unless someone is
outdoing you with the other answer I aske


Accepted Solution

falter earned 200 total points
ID: 2824639
>alos how can I say have a class where I
>will put a load of values which I will
>use in all classes do I have to make
>the all public of is this a easy way?

If you wnt to use these values only as constants (read only) than the easiest way is to define them like this
public final int myConst1 = 5;
and access them with:

If you want to modify these values than I would prefer to make them private and code public methods for getting and setting them.

This will allow you to put in synchronizing code needed in a multithreaded environment.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 70
Is there a simpler dropbox system? 10 53
Java Eclipse Loop 3 48
Java class and jar 3 61
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question