donderdag 27 september 2012

keep on scoping on different packages

Introduction:

Scoping the methods and constructors the right way can be quite a headache. In a lot of libraries I see that most of the classes are public accessable. This makes it for the user of those libraries confusing to use. So to keep myself in shape I write this little reminder.

Scoping
  1. Public methods and constructors are accesible everywhere in your application and also in the applications that are using your application as a library. 
  2. Protected methods are only accesible inside the package where the classes are and outside when inherited.
  3. Package aka scope or friendly, is only accesible inside the package including inherited. 
  4. Private is only accesible inside the class scope.
  5. Local is only accesible inside the method where it is declared. 
 Examples

Public class Scoping{
   public String publicScope; 
   protected String protectedScope; 
   String packageScope;
   private privateScope; 
}

If you write a library that can be used by others, I think with the design, the word userFriendly should be considered.
Beside good and working examples and completed documentation scoping can be helpfull. If your contract is one of the few things which is accesible, mistakes by your users (other programmers) are made less.

One of the last things I show as an example is a small trick to avoid using public when you are using classes from different packages:

package example.superclass;

public abstract class SuperClass{
  protected String protectedScope;

   protected String getProtectedScope{
     return this.protectedScope;
  }

  protected String setPrivateScope(String privateScope){
    this.protectedScope = protectedScope;
  }

 }

package example.subclass;

public  class SubClass{
   protected String getProtectedScope{
     return super.protectedScope;
  }

  protected String setPrivateScope(String privateScope){
    super.protectedScope = protectedScope;
  }

 }

package example.superclass;
public class callSubClassThroughSuperClass{
   SuperClass class = new SubClass();
   public static void main(String[] args){
      class.setProtectedScope("scope has been set");
      System.out.println(class.getProtectedScope);
   }
}
As you noticed are the class callSubClassThroughSuperClass and the SuperClass in the same package. This makes the protected methods of the SuperClass accesible for this class. The SubClass
is in a different package and accesses the protected methods through inheritence. This way you can bridge the distance between two packages and still use a scoping that protects usages from the outisde.

Have fun!

woensdag 19 september 2012

split a String and keep the token

Sometimes you want to split a String but still need the token for finding the parts you need. I setup an example to deal with this:

You have a String that contains a list of friends. All you want is to print out those names and not the first part of the String. 
    private void checkNames(){
        String name = "This is a list with friends, Fred, Ted, Robert,Maria, Roberta, Kaya";
        String splitExp = "(?=[:,])"; //The last char in the brackets [] is the token in our case the comma.
        String[] splittedNames = name.split(splitExp);
        for (int i = 0; i < splittedNames.length; i++) {
            if(splittedNames[i].startsWith(",")){
                //print it remove the comma and spaces
                System.out.println(splittedNames[i].substring(1).trim());
            }
}
       
This will give you a clean list of your friends names.
Have fun.