1. Are you ready for the Galaxy S20? Here is everything we know so far!

Best practices in regards to methods

Discussion in 'Android Development' started by Atkinson_88, Dec 8, 2013.

  1. Atkinson_88

    Atkinson_88 Newbie
    Thread Starter

    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.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. alostpacket

    alostpacket Over Macho Grande?

    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]
     
    Atkinson_88 likes this.
Loading...

Share This Page

Loading...