By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Class in another file

P: n/a
Hi,

I am very (very very) new to java and have what I am sure is a simple
problem that I hope someone can help with. I have two .java files. The first
has

public final class FormatSWN {

public FormatSWN() throws Exception {

etc etc

}

etc etc
}

This file compiles without an errors. I am wanting to call the FormatSWN
class from within another file. The code in that file is

class CallSWN {
public static void main(String[] cName) {

FormatSWN();
}
}

This file returns a compile error saying "Cannot resolve symbol" and has the
^ pointing at the start of FormatSWN();

Both these files are in the same directory so that should not be an issue.

I know this will be really simple, but it has me stumped. If someone has the
answer, that would be great

Steve


Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
S. McKee skrev:
Hi,

I am very (very very) new to java and have what I am sure is a simple
problem that I hope someone can help with. I have two .java files. The first
has

public final class FormatSWN {

public FormatSWN() throws Exception {

etc etc

}

etc etc
}

This file compiles without an errors. I am wanting to call the FormatSWN
class from within another file. The code in that file is

class CallSWN {
public static void main(String[] cName) {

FormatSWN();
}
}

This file returns a compile error saying "Cannot resolve symbol" and has the
^ pointing at the start of FormatSWN();

Both these files are in the same directory so that should not be an issue.

I know this will be really simple, but it has me stumped. If someone has the
answer, that would be great

Steve



FormatSWN is a class, it is the "blueprint" for an object of type
FormatSWN. You have to create an *instance* (object) of the class before
tying to call any of its methods. Like so:

FormatSWN myFormatSWNObject = new FormatSWN();

Now you can call methods using your new object:

myFormatSWNObject.aMethodInFormatSWN();

(The identifier 'myFormatSWNObject' is what you decide to call your new
object. The method 'aMethodInFormatSWN' is a method (you) defined in
class FormatSWN.)

/ulf
Jul 18 '05 #2

P: n/a
Thanks !!

I also managed to make it work by

try {
new FormatSWN(new String[]{cName[0]});
} catch etc etc

I have one more question if I may. In the FormatSWN class I have a method
called Speakable, that has to use the Vector command to turn something like
123 into one, two, three.

The method is called by

Vector speakable = Speakable(strinput); //strinput is from the call to
FormatSWN (see original post below)

The Speakable method has

Vector Speakable (String[] strinput) {
int i;
int x = 0;
String strword;
Vector strspeakable = new Vector();

for (i=0; i < strinput.length; i++) {
x = Integer.parseInt(strinput[i])

some switch commands on x & a strspeakable.add go here
}

My problem is that the first time in I would expect strinput.length = 3 and
x to equal 1 (if 123 is passed), then 2, then 3 etc, but instead the
strinput.length =1 and x = 123 ?

Can you tell me what is wrong ?

Thanks and sorry for asking obvious questions !

Steve

"Ulf_N" <ul*@dontlikespam.se> wrote in message
news:Mb*********************@newsc.telia.net...
S. McKee skrev:
Hi,

I am very (very very) new to java and have what I am sure is a simple
problem that I hope someone can help with. I have two .java files. The first has

public final class FormatSWN {

public FormatSWN() throws Exception {

etc etc

}

etc etc
}

This file compiles without an errors. I am wanting to call the FormatSWN
class from within another file. The code in that file is

class CallSWN {
public static void main(String[] cName) {

FormatSWN();
}
}

This file returns a compile error saying "Cannot resolve symbol" and has the ^ pointing at the start of FormatSWN();

Both these files are in the same directory so that should not be an issue.
I know this will be really simple, but it has me stumped. If someone has the answer, that would be great

Steve



FormatSWN is a class, it is the "blueprint" for an object of type
FormatSWN. You have to create an *instance* (object) of the class before
tying to call any of its methods. Like so:

FormatSWN myFormatSWNObject = new FormatSWN();

Now you can call methods using your new object:

myFormatSWNObject.aMethodInFormatSWN();

(The identifier 'myFormatSWNObject' is what you decide to call your new
object. The method 'aMethodInFormatSWN' is a method (you) defined in
class FormatSWN.)

/ulf

Jul 18 '05 #3

P: n/a
S. McKee skrev:
<snip>

Vector Speakable (String[] strinput) {
int i;
int x = 0;
String strword;
Vector strspeakable = new Vector();

for (i=0; i < strinput.length; i++) {
x = Integer.parseInt(strinput[i])

some switch commands on x & a strspeakable.add go here
}

My problem is that the first time in I would expect strinput.length = 3 and
x to equal 1 (if 123 is passed), then 2, then 3 etc, but instead the
strinput.length =1 and x = 123 ?


Your result indicate that strinput only contains one string, in the
first element, and that string is "123". It should contain *three*
elements, holding the strings "1", "2", and "3", respectively. (I think
that you perhaps should read a bit more about how classes, objects,
arrays, etc. works in java. It's boring, I know, but it will save you
lots of time. I promise.) /ulf
Jul 18 '05 #4

P: n/a

You should look at what the difference between the string "123" and the
value returned from "123".split("") is. The first is a string with
three characters and the second is an array that contains four strings,
"", "1", "2", "3". Contemplation on the difference should be
illuminating.

On style, I would recommend that you use a List instead of a Vector. A
List is a data type from the Collections framework which provides a
much better overall selection of classes and allows you to choose when
to use thread synchronization.

Another point is that you seem to be trying to do something useful in a
constructor. If you aren't trying to create an object to use later,
you might be better off using a static method. I should point out that
using static methods is often an indication of poor design.

Jul 18 '05 #5

P: n/a
> Newsgroups: comp.lang.java
(Added comp.lang.java.programmer because comp.lang.java is not valid.)
From: "Ted Dunning" <ted.dunn...@gmail.com>
I should point out that using static methods is often an indication
of poor design.


I respectifully disagree with your generalization. If you are defining
a new data structure, different from anything already available in the
Java API, then it's appropriate to have one or more constructors to
create such structures and one or more methods to provide access or
mutation upon such structures. But if you are merely defining methods
that work with already-existing data structures and/or primitive data
types, that were defined in classes whose source you are not allowed to
modify (and might not even be able to see), then it is impossible for
you to make instance methods for processing those objects, so you have
no choice but to use static methods, which are completely appropriate
for such use.

Note: You might still choose to sub-class the API class, not define any
new data members if you don't need any, but do define additional
instance methods that are available only for types of your sub-class.
One problem with that tactic is that you can't directly apply your
methods to objects of the base class, as you can for static methods.
You can't even down-cast objects of the base class to apply your
derived-class instance methods to them. You need to construct objects
*originally* of your derived class, then you can apply methods of both
base and derived classes. Or if you are given an object of the base
class you need to pick apart all the data members and re-build an
object of your derived class, a royal pain. Better to just use a static
method which can be directly applied to base-class objects.

What indicates poor design is using an inappropriate type of method,
static method when instance method is more appropriate, or vice versa.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.