Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Parameter-passing to another class

Thread view: 
Gunnar Skandsen - 08 Apr 2004 21:53 GMT
I have to classes, classA and classB. I want classA to 'call' classB
and pass a parameter (String).

I was thinking I could do something like this (within classA): classB
B = new  classB (parameter);

But this doesn' compile.
Andrew Thompson - 08 Apr 2004 21:17 GMT
> I have to classes, classA and classB.

These are not good names for your two classes.
ClassA and ClassB would folow the naming convetion
used by most Java programmers, but they are not
descriptive names.

Something more like WhatItIsWhatItInheritsFrom

So, maybe..
FactorialGUIFrame and..
Factorial (shrugs, I hope you get the idea)

>..I want classA to 'call' classB
> and pass a parameter (String).
>
> I was thinking I could do something like this (within classA): classB
> B = new  classB (parameter);

// you must declare it first
classB B = new  classB (parameter);

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Gunnar Skandsen - 08 Apr 2004 23:44 GMT
Ok, my names were  maybe not good.

CompanySetup and CompanyReplicate are maybe more descriptive names.
CompanySetup should 'call' CompanyReplicate and provide this class
with the companyCode (to replicate).

I was thinking of something like this (in CompanySetup):

String companyCode = "K1";
CompanyReplicate replicator = new CompanyReplicate(companyCode);

- But it's not correct (doesn't compile).

>> I have to classes, classA and classB.
>
>These are not good names for your two classes.
>ClassA and ClassB would folow the naming convetion
>used by most Java programmers, but they are not
>descriptive names.

>Something more like WhatItIsWhatItInheritsFrom
>
[quoted text clipped - 12 lines]
>
>HTH
Andrew Thompson - 08 Apr 2004 23:01 GMT
> Ok, my names were  maybe not good.
>
> CompanySetup and CompanyReplicate are maybe more descriptive names.

Much more descriptive, and perhaps
indicating you are taking the wrong
approach.

Company _class_ should have _methods_
companySetup() (possibly a protected
method called by the constructor) and
maybe cloneCompany().. that second one
is iffy.  IT is unlikely you will actually
need to clone an existing Company object.

> CompanySetup should 'call' CompanyReplicate and provide this class
> with the companyCode (to replicate).
[quoted text clipped - 5 lines]
>
> - But it's not correct (doesn't compile).

OK.  Time for a more rigorous approach,
give us a comlete example.
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Gunnar Skandsen - 09 Apr 2004 20:48 GMT
I think I've got it. This code for CompanySetup-class will do what I
was looking for:

String companyCode = "K1";
CompanyReplicate replicator = new CompanyReplicate();
replicator.main(companyCode);

- Assuming the main-method in CompanyReplicate-class takes
a string as parameter.  

>Ok, my names were  maybe not good.
>
[quoted text clipped - 32 lines]
>>
>>HTH
Bryce (Work) - 09 Apr 2004 20:23 GMT
>I think I've got it. This code for CompanySetup-class will do what I
>was looking for:
[quoted text clipped - 5 lines]
>- Assuming the main-method in CompanyReplicate-class takes
>a string as parameter.  

I think you are missing the concept of constructors. If you don't
specify a constructor, the class will have a default constructor.
i.e., no parameters passed. In your case, CompanyReplicate will need a
constructor that accepts a String object, such as:

class CompanyReplicae{
 CompanyReplicate(String message) {
 ... do something with parameter here.
 }

Or, you can have a property/method that your calling class uses...

>>Ok, my names were  maybe not good.
>>
[quoted text clipped - 32 lines]
>>>
>>>HTH

--
now with more cowbell
Bryce (Work) - 09 Apr 2004 20:28 GMT
>I think I've got it. This code for CompanySetup-class will do what I
>was looking for:
[quoted text clipped - 5 lines]
>- Assuming the main-method in CompanyReplicate-class takes
>a string as parameter.  

Actually, yea, that does work. At first I thought you were calling the
static main method of the CompanyReplicate class. IMHO, main isn't a
good name for a method, as it could be easily confused with the static
main class.

--
now with more cowbell
Andrew Thompson - 09 Apr 2004 21:42 GMT
> At first I thought you were calling the
> static main method of the CompanyReplicate class. IMHO, main isn't a
> good name for a method, as it could be easily confused with the static
> main class.

The OP needs to have a careful look at
the source written by other Java programmers,
to see the way we generally name and indicate
classes, methods, and importatnt things like
'main' and 'init'.  

It is confusing the way it is written now.

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Gunnar Skandsen - 09 Apr 2004 22:52 GMT
>>I think I've got it. This code for CompanySetup-class will do what I
>>was looking for:
[quoted text clipped - 8 lines]
>Actually, yea, that does work. At first I thought you were calling the
>static main method of the CompanyReplicate class.

I do in fact call the static main method of CompanyReplicate. I
changed this metod to take one string as parameter. - And I was a bit
supprised that it worked.

> IMHO, main isn't a
>good name for a method, as it could be easily confused with the static
>main class.
Andrew Thompson - 09 Apr 2004 22:10 GMT
>>>I think I've got it. This code for CompanySetup-class will do what I
>>>was looking for:
[quoted text clipped - 12 lines]
> changed this metod to take one string as parameter. - And I was a bit
> supprised that it worked.

(sigh) You have created a new method,
it is not THE

public static void main(String[] args)

method, you have instead created
a new method something like..

public static void main(String whateverYouCalledIt)

This is a different and distinct method
from THE 'main'..

I go back to something I said before,
a _complete_ example please, so that
we can see the (possibly number of)
ways you are going wrong.  You are slightly
out of your depth if you have multiple classes
but do not understand polymorphic method
signatures and what constructors do/are for..
<http://www.physci.org/codes/sscce.jsp>

<mock stern>
I do not want to see more code snippets from
you until you can compile them through..
<http://www.physci.org/javac.jsp>
</mock stern>

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.