Solved

classes in a package talking to each other

Posted on 2000-05-16
8
169 Views
Last Modified: 2010-04-01
so say if I have few classes all in
/a/dir/where/classes/are
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
0
Comment
Question by:thedogeater
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:falter
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
                  a.dir.where.classes.are.mytest

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
classname.method(...)
0
 
LVL 4

Expert Comment

by:kylar
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

com.kylar.Toolbox.MyClass;

when I mean

com.kylar.ToolBox.MyClass;

:)
Cheers
Kylar
0
 

Author Comment

by:thedogeater
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";
    trying.tocall(s);
  }
}

and in a new file in same dir

package my.package

public class trying{
  public static void tocall (String meg){
    System.out.println(meg);
  }
}

kind of thing what is it I am doing wrong?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:thedogeater
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:+)

Thanks
0
 

Author Comment

by:thedogeater
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:+)

Thanks
0
 
LVL 3

Expert Comment

by:falter
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/atest.java
- compiling javac my/test/trying.java

calling
java my.test.atest
result:
why not is working

0
 

Author Comment

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


Thanks
0
 
LVL 3

Accepted Solution

by:
falter earned 200 total points
ID: 2824639
thedogeater,
>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:
MyClass.myConst1

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.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java stored proc example 9 29
MySqlDump not dumping triggers 1 31
hibernate jars 4 29
Java string replace 11 46
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

770 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