473,657 Members | 2,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class in another file

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
5 5216
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 myFormatSWNObje ct = new FormatSWN();

Now you can call methods using your new object:

myFormatSWNObje ct.aMethodInFor matSWN();

(The identifier 'myFormatSWNObj ect' is what you decide to call your new
object. The method 'aMethodInForma tSWN' is a method (you) defined in
class FormatSWN.)

/ulf
Jul 18 '05 #2
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(strin put); //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.parseIn t(strinput[i])

some switch commands on x & a strspeakable.ad d 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*@dontlikesp am.se> wrote in message
news:Mb******** *************@n ewsc.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 myFormatSWNObje ct = new FormatSWN();

Now you can call methods using your new object:

myFormatSWNObje ct.aMethodInFor matSWN();

(The identifier 'myFormatSWNObj ect' is what you decide to call your new
object. The method 'aMethodInForma tSWN' is a method (you) defined in
class FormatSWN.)

/ulf

Jul 18 '05 #3
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.parseIn t(strinput[i])

some switch commands on x & a strspeakable.ad d 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

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
> Newsgroups: comp.lang.java
(Added comp.lang.java. programmer because comp.lang.java is not valid.)
From: "Ted Dunning" <ted.dunn...@gm ail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3947
by: Ryan Mitchley | last post by:
Hi I have a rather complicated class for performing matrix operations using complex arithmetic. I have written two classes, CComplexMatrix and CComplexMatrixTemp, and have been attempting to remove unnecessary object copies by converting to the temp class for intermediate operations. The temp class reuses allocated space as is necessary.
10
2528
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having class definitions etc. in one big file (yay, major discovery...). My problem is this: When I define a class in one include file, and then try to instantiate it in another, I get compile-time errors saying the type is invalid. If i move the...
7
12960
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
2
2330
by: Nik | last post by:
Hi, I'm trying (and failing) to learn MC++ and I get the feeling that when I ask this question, the proverbial record will scratch to a halt and everyone will stare at me as though I'm totally weird, but here goes... Using VS.NET 2003, I have created a MC++ .NET Class Library project. I have create a class in the default header file created by the project.
7
4654
by: tshad | last post by:
I have a problem with a VS 2003 project. This project was designed and works fine in VS 2003. But trying to open the project I get the following error. ************************************************************ The class EmailPoller can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try...
4
1747
by: asterixgallier | last post by:
Hello at all, i've got the following problem: - I define a class template in a header file - I implement the methods in a cpp file - I want to build an instance of my class template this works if the implementation of the constructor is either inside the class or in the same file as the class. But if the implementation is outside in another file, the linker complains, that there is an
4
2047
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few other things. What would be a good design to accomplish these goals? Here are the ideas that I have come up with: Class X = class with data that I want to put into csv. 1. Nested Function Object use a nested function object to do the conversion.
32
5803
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many classes in a Python module/file. What is the motivation behind it, would it be a bad idea to have a guideline in your project that promotes a one class per file structure (assuming most of the programmers a background similar to mine)?
19
2417
by: active | last post by:
The ColorPalette class has no constructor so how does one use it? I define a variable by: Dim cp as ColorPalette but don't know how assign an object to the variable. Thanks in advance
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.