• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Best practices in regards to methods

Hello,

I'm currently learning Android development from online videos, the videos im watching show all the code example in one file i.e the .java so it doesn't confuse the user when trying to learn, my question is in regards to class that have methods inside them i.e connection to mysql, connecting to a WCF service etc

From what i've done so far I created a .java class inside

Appname > Src > com.example.appname called connectToWcf.java

Below is the class

[HIGH]
package com.example.newapplication;

public class connectToWcf {


public connectToWcf() {
// TODO Auto-generated constructor stub
}

public boolean connectToWcf(String name, boolean showAge, String postCode) {


return true;

}


}

[/HIGH]So one constructor, and one method which returns a type of boolean called connecToWcf

In my other classes which have activity's (pages) assigned to them i can then call the above from any other .java class as follows

[HIGH]
public void onClick(View v) {

connectToWcf Method = new connectToWcf();

boolean Success = Method.connectToWcf("Hello", true, "po156rt");

if(Success)
{
//Webmethod Great

}
else
{

}
}
[/HIGH]The question i have to ask is the above the correct way i should go about setting up global methods within my android application ?

As always thank you for your time.
 
You need to start using java style standards ;)


Constructors and Classes and Type are TitleCase.

fields, methods and local variables are camelCase

If you need a method that can be accessed from outside of an instance of your class you can make it static.

Have a look at my stack overflow answer here:
Declare a global variable, or declare multiple times in each class - Stack Overflow


[HIGH]
package com.example.newapplication;

public class RestConnector {


public RestConnector () {
// constructor
}

public static boolean connect(String name, boolean showAge, String postCode) {
return true;
}
} [/HIGH]

[HIGH]
public void onClick(View v) {
boolean success = RestConnector.connect("Hello", true, "po156rt");
if(success)
{
//success!
}
}[/HIGH]
 
  • Like
Reactions: Atkinson_88
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones