0

Although it seems easy but I stuck my head down for a while.

So I have my class Common.java as:

    package com.spil.util;
       public class Common extends Fragment implements SubscriptionListener
         {
           String WEBSERVICE = "http://webservice.carrierservice.com/";  
         }

And the class DownloadDataActivity.java as:

package com.spil.main;
public class DownloadDataActivity extends TimerTask {
String WEBSERVICE = "http://webservice.carrierservice.com/";    
}

I don't want the string WEBSERVICE to be repeated so I thought to access it from Common.java.

So I changed Common.java to have string WEBSERVICE as:

  public static String WEBSERVICE = "http://webservice.carrierservice.com/"; 

And then in DownloadDataActivity.java, I am accessing it as:

public static String WEBSERVICE = Common.WEBSERVICE ;

This works fine but my question is:

Is it a good practice to change string variable to public static string variable just to access it in another class and how it will affect the scope as I assume that making this url as public will allow it to be accessible from anywhere ?

But I didn't find any alternative than to just define the URL twice in each class.

steveax
  • 17,527
  • 6
  • 44
  • 59
sjain
  • 23,126
  • 28
  • 107
  • 185

2 Answers2

2

Well, you could declare a constant String and you can keep it inside Constants class which is accessed at application level.

public final class Constants {
    public static final String WEBSERVICE = "http://webservice.carrierservice.com/";
    // other application level constants
}

Access constant anywhere in your application using static import as :

package com.spil.main;

import static comp.app.util.Constants.WEBSERVICE;

public class DownloadDataActivity extends TimerTask {
   someMethod() {
      makeCallToService(WEBSERVICE, otherParams);
   }
}

This would be the right approach.

To use constants, good practice would be to use constants in respective classes if those constants are not going to be used anywhere.

If there are constants to be accessed from all over application, you can define those constants in one class like above and use above approach.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • That is reasonable but changing the sensitive url scope from `String WEBSERVICE=` to `public static final String WEBSERVICE=` just to use it in another class seems good? It will not be modified because it is final but it is accessible since this is now public. – sjain Mar 06 '13 at 09:48
  • @NandkumarTekale: IMHO for sensitive information, public exposure is not advisable over redundancy. (For information like credential its good to add few more lines and make it more secure than making it public). PS: Its just my opinion and not best practice etc. – Ajinkya Mar 06 '13 at 10:00
  • public exposure of constants means if you are having interface and you are exposing that interface as an API contract in that case what you say is fine. In above case, you are using class and implementation is in your hand. – Nandkumar Tekale Mar 06 '13 at 10:13
  • Thanks, your last comment is actually what I wanted. The answer added a value. – sjain Mar 06 '13 at 11:29
1

Yes public will make it accessible from anywhere in your application.
It depends if the string contains some information which you don't want to expose to other part of code. You don't want other developers (or yourself) to access it even accidentally/unintentionally from anywhere else then make it private.
Otherwise if even unintended access not going to make any harm then its fine to make it public so you will avoid few lines of redundant code.
Second part is if you want to allows other parts to update your attributes. Also if you want to use the same value for String then make it final.
This might help Class attribute declaration: private vs public

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161