Java GUI Problem

B

Bjorn Abelli

...
Is it possible for a class inside of a package to access
classes outside of its package?

Of course, but only if those other classes belongs to another *named*
package.
I do not know how to do this and I have been trying to find it via
google but no luck at all.

I believe you've already done that.

Not that you showed those parts of your own code, but I guess you've used
the "import" statements?

That's how it's done.

// Bjorn A
 
T

Timo Stamm

DyslexicAnaboko said:
Mr. Wong, you seem to be very good at this, possibly you can help me
out with another problem I just ran into:

Hm. I'm not Oliver. Do you accept an answer from me too?

Is it possible for a class inside of a package to access classes
outside of its package?
Yes.


I do not know how to do this and I have been trying to find it via
google but no luck at all.

That's probably because the question is so very basic. Have you ever
learned the language basics? This tutorial from sun should help you out:

http://java.sun.com/docs/books/tutorial/java/index.html


Timo
 
A

Alex Hunsley

DyslexicAnaboko said:
Thanks for the long reply that won't help me with anything at all.

It won't help you because of your attitude, not because there's no
information there.
I am
just trying to get questions answered and I don't have time to have a
flame war. So please, if you aren't going to help me understand what I
did wrong and just ridicule me, I would rather you save your time and
mine by not replying.

Sheesh. I posted a good deal of useful information, containing
*constructive* criticism. And you seem to think I'm just getting at you
- I'm not.
Do you know the different between constructive criticism and flaming?

I think perhaps you misunderstand the nature of getting help on Usenet a
little: people don't fart about with couching feedback in the most sugar
coated way possible. It's easier just to be direct with feedback and
this is what people do. It doesn't mean they're being offensive or
flaming you, it's just that it's the easiest way to do things. And if
someone provides useful information back, even if it's not directly
useful to you in what you're doing now, it doesn't make sense to gripe
about it. You paid nothing for it, and griping makes the person who
replied less likely to reply in the future.
And people do often post feedback on aspects of your code that you
weren't concerned about. Sorry if that irks your sense of entitlement,
but that's how it is. It's free information, and can be useful to
others, so don't moan about it.

As for saving your time and mine by not reply... The beauty of usenet is
that even though you didn't get anything out of the reply/discussion,
other people can. So it wasn't a waste of time, even though you yourself
apparently didn't get anything out of it.
 
T

Thomas Weidenfeller

DyslexicAnaboko said:
Thanks for the long reply that won't help me with anything at all. I am
just trying to get questions answered and I don't have time to have a
flame war. So please, if you aren't going to help me understand what I
did wrong and just ridicule me, I would rather you save your time and
mine by not replying.

If you can't stand discussions, don't ask in a discussion group.

/Thomas
 
O

Oliver Wong

DyslexicAnaboko said:
Mr. Wong, you seem to be very good at this, possibly you can help me
out with another problem I just ran into:

Is it possible for a class inside of a package to access classes
outside of its package?

I do not know how to do this and I have been trying to find it via
google but no luck at all.

If you could unlock that mystery it would help me a great deal.

Thank you for your help thus far.

As other have mentioned the way to do it is with an "import" statement.
At the top of your class file, you write "import" followed by the fully
qualified name of the class. For example, if I wanted to make the class
"ArrayList" in the package "java.util" available to my file, even though my
file isn't part of the "java.util" package, I'd write "import
java.util.ArrayList;"

Note that this only works if the class I'm trying to import is part of a
package. If you have "packageless" classes, then you can't import them.

- Oliver
 
D

DyslexicAnaboko

I'm glad everyone thinks I am a beginner and I know I am at fault for
not explaining my problem fully. I know what an import is, that is not
the problem. I know how to import a class from a package. All I was
asking was whether or not a class inside of a package that I myself
created would be able to access a class outside of its package that I,
again, myself created. This is not a 1 2 3 operation. There is no
java.util.classIjustCreated, that only works for when you are using
packages and classes from java or if you create your own, but I am not
going to do that.
From what I understand, people are saying that packages can access one
another.

So lets say I have:

package alpha ;
package beta ;

and they both reside in C:\greekStuff\

then technically any of the classes inside those packages will be able
to:
A. Import each other's package
B. Since the package is imported use the classes inside of it.

===================================================
However, this below is what I am asking and I am afraid won't work:

Okay, again lets say I have:

package alpha ;
class beta ;

and they both reside in C:\greekStuff\
meaning their file paths are:
C:\greekStuff\alpha\ (alpha classes in here)
C:\greekStuff\beta.java

Then what I want to do is:

A. let a class inside of the alpha package be able to use class beta.
To be more clear, I want alpha classes to be able to access methods in
class beta. Create objects and do what ever you would normally be able
to do if you just had two classes in a folder together.

For the people who are only paying attention to how I phrased
everything, tell me what you don't understand and I will elaborate
again the best I can.
 
S

Steve Wampler

DyslexicAnaboko said:
However, this below is what I am asking and I am afraid won't work:

Okay, again lets say I have:

package alpha ;
class beta ;

and they both reside in C:\greekStuff\
meaning their file paths are:
C:\greekStuff\alpha\ (alpha classes in here)
C:\greekStuff\beta.java

Then what I want to do is:

A. let a class inside of the alpha package be able to use class beta.
To be more clear, I want alpha classes to be able to access methods in
class beta. Create objects and do what ever you would normally be able
to do if you just had two classes in a folder together.

For the people who are only paying attention to how I phrased
everything, tell me what you don't understand and I will elaborate
again the best I can.

This will work fine as long as you have C:\greekstuff\ on your
classpath. Java will look for classes and packages in the
directories on your classpath.
 
T

Thomas Fritsch

DyslexicAnaboko said:
Is it possible for a class inside of a package to access classes
outside of its package?

I do not know how to do this and I have been trying to find it via
google but no luck at all.

If you could unlock that mystery it would help me a great deal.
Sure it is possible. Actually it is so common, that every real-life program
does it all the time.

Here is an example:

// file pkg1/A.java
package pkg1;
import pkg2.B;

public class A {
public doSomething() {
B b = ...; // get or create an instance of B
b.doSomethingElse();
}
}


// file pkg2/B.java
package pkg2;

public class B {
public doSomethingElse() {
//...
}
// ...other methods and constructors
}

All this is described near the beginning of every good java text-book. It is
therefore important to work through the basics from the very beginning,
instead of jumping into the advanced features first.
(By the way: I don't mean this as an offence, but as an honest
recommendation how to proceed.)
 
O

Oliver Wong

DyslexicAnaboko said:
I'm glad everyone thinks I am a beginner and I know I am at fault for
not explaining my problem fully. I know what an import is, that is not
the problem. I know how to import a class from a package. All I was
asking was whether or not a class inside of a package that I myself
created would be able to access a class outside of its package that I,
again, myself created. This is not a 1 2 3 operation. There is no
java.util.classIjustCreated, that only works for when you are using
packages and classes from java or if you create your own, but I am not
going to do that.

I believe my post, and at least one other post, mentioned that you
cannot import a class if it's not part of a well specified package. This is
one of the reasons it's recommended that you always put your classes in
packages.
another.

So lets say I have:

package alpha ;
package beta ;

and they both reside in C:\greekStuff\

then technically any of the classes inside those packages will be able
to:
A. Import each other's package
B. Since the package is imported use the classes inside of it.

Technically, you don't import packages; rather you import classes. With
Java 1.5, there's something called "static import" which lets you import
static methods, but that's a completely different topic. When you see
something like "import java.util.*", I'd think of it NOT as importing the
"java.util" package itself, but rather as importing all the classes within
that package.
===================================================
However, this below is what I am asking and I am afraid won't work:

Okay, again lets say I have:

package alpha ;
class beta ;

and they both reside in C:\greekStuff\
meaning their file paths are:
C:\greekStuff\alpha\ (alpha classes in here)
C:\greekStuff\beta.java

Then what I want to do is:

A. let a class inside of the alpha package be able to use class beta.
To be more clear, I want alpha classes to be able to access methods in
class beta. Create objects and do what ever you would normally be able
to do if you just had two classes in a folder together.

Yes, this won't work. You need to put class "beta" in a package (which
can also be called "beta" if you want). Note that it's a good idea to start
your class names with an uppercase letter (e.g. "Beta"), and your packages
with a lowercase letter. So if you put class "Beta" in a package called
"beta", then to access it from within package "alpha", you'd have a line
like "import beta.Beta;"

- Oliver
 
S

Steve Wampler

Steve said:
....
This will work fine as long as you have C:\greekstuff\ on your
classpath. Java will look for classes and packages in the
directories on your classpath.

Sorry. I'm wrong (failing memory). You can't do this without putting
class beta inside a named package, as others have said.
 
D

DyslexicAnaboko

None taken, I know what you mean, its just that I am taking this class
that doesn't really care whether or not you have learned this much yet.
I am not familiar with all these things, so it is good to learn, that
is why I ask questions. I am taking Software Engineering and I had to
learn a lot on my own to get my program to work, nothing I learned in
the other three classes I took previously. I always try to keep up with
these things, but it is only done through practice.

So thanks to everyone who is helping me, I really do appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top