How do you call public void from another .java?

Put your problem here if it does not fit any of the other categories.

How do you call public void from another .java?

Postby g33k499 » Mon Dec 21, 2009 6:38 pm

Here is what I am working with. I have a file called recsms.java and another called manager.java.

In manager.java, i have a public void called WriteSettings. In recsms.java, I have code to read incoming sms data.

I need to be able to call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. WriteSettings(this, smsdata);
  3.  
  4.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


Located in manager.java from recsms.java.

Thanks,
Adam
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Top

Postby g33k499 » Mon Dec 21, 2009 8:25 pm

UPDATE

I found what I needed to do to make it work, but now I have new issues.

I is possible to call,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. manager.WriteSettings(context,data)
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


But I get errors about making a static reference to a non-static method. But, I can't use a static method with my context calls and variables for my .txt files.

Any thoughts?
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Postby qlimax » Mon Dec 21, 2009 9:00 pm

we need more details like:

-the declaration of the writesettings method


btw

--basically if you want to call a method of another class, without making this method static ,you have to first create an object of that class. and then call this method on the object

--you cannot use non-static fields in a static method


bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SteffenL » Mon Dec 21, 2009 9:02 pm

Edit: Seems like I was a bit too slow to write my reply. :)

First, class names should begin with a capital letter (Manager). It is of course up to you, though. :)

Anyhow, you have two options:
  • Make the method static and call it from anywhere without creating an instance of Manager first.
  • Create an instance of Manager and then call the method.


Example using static method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static void WriteSettings(...)
  2.  
  3. {
  4.  
  5.   ...
  6.  
  7. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Call it like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Manager.WriteSettings(...);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Example using non-static method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void WriteSettings(...)
  2.  
  3. {
  4.  
  5.   ...
  6.  
  7. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Call it like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Manager manager = new Manager();
  2.  
  3. manager.WriteSettings(...);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Does that help you?
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby g33k499 » Mon Dec 21, 2009 9:18 pm

Thanks again Steffen! This the 2nd pickle you got me out of!

I will use Capital class names from now on. I just went with what Eclipse started me off with and now I am just too lazy to change it.

This is my first app, so I am bound to have some noobness in most aspects of my code. I will learn though.
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Postby qlimax » Mon Dec 21, 2009 9:19 pm

:lol: steffen , [marq=up]your answer[/marq] is more accurated :uarrow:


:xmas: :xmas: :xmas:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby qlimax » Mon Dec 21, 2009 9:21 pm

ah more...

method names should begin with lowercase letter :P

bye and good luck g33k499
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SteffenL » Mon Dec 21, 2009 9:53 pm

qlimax wrote:...

You gave a good answer yourself! :D

g33k499 wrote:...

Glad we could help! :D

There are many rules (or rather suggestions) on how to write code. Some good, some not so good.
My personal opinion is that public methods should begin with a capital letter (but private ones should not) (there is more but I should not bring that up now, and here), but it may be that the "standard" for Java code is otherwise, even if the code could look better. I personally think the code in Android SDK could look better. :lol:
Last edited by SteffenL on Mon Dec 21, 2009 10:15 pm, edited 1 time in total.
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby qlimax » Mon Dec 21, 2009 10:01 pm

¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SteffenL » Mon Dec 21, 2009 10:05 pm

qlimax wrote:...

They are actually just guidelines for writing understandable code, not the way you have to write it. There are many ways to write code.
I do think it would be a good to follow a certain style so that it is easier to share code and work on it together (and even understand your own code yourself).
The way on that site may be the way most people have gotten used to (for Java) for all I know.
I think that code styles should not change (a lot) no matter what language it is, so it would be natural at least for me to write as tidy as I usually do in any language (and not "de-tidying" it because it is a different language). :)
Last edited by SteffenL on Mon Dec 21, 2009 10:19 pm, edited 3 times in total.
Regards,
Steffen
SteffenL
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed Dec 16, 2009 9:52 am

Postby qlimax » Mon Dec 21, 2009 10:10 pm

I know...
yes you can add a GOODMORNING before every method :P lol

btw
[SOLVED] and merry Xmass :xmas:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 17 guests