我应该为常量使用哪种访问修饰符?

时间:2015-10-01 02:48:04

标签: java coding-style

public static final

private static final

考虑仅从类本身使用常量的情况。哪种风格更合适?

5 个答案:

答案 0 :(得分:1)

如果您只需要在课程中使用这些值,则它们应为private。通常会使事情尽可能无法访问,并且只在必要时才增加可访问性。

答案 1 :(得分:1)

使用多个类时应使用

private。如果需要访问其值,可以使用getter。

public只有在你只有一个班级时才能真正使用。

答案 2 :(得分:0)

为什么不懒惰,只做

{{1}}

我认为开始使用' package'访问;你可能至少需要在单元测试中使用它。然后,如果必须,您可以将其更改为公共/私人或受保护。

'私人'通常没有必要 - 你很少需要担心同一个包下的其他类可能搞砸了这个类。

答案 3 :(得分:0)

为什么不使用 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <widget id="au.gov.nal.hearing.myhearing" version="0.7.9" versionCode="607" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:gap="http://phonegap.com/ns/1.0"> <gap:platform name="ios"/> <gap:platform name="android"/> <name>My Hearing</name> <description>My Hearing App</description> <author email="rene@bywave.com.au" href="http://bywave.com.au/">Renemari Padillo</author> <content src="index.html"/> <preference name="AutoHideSplashScreen" value="true"/> <preference name="SplashScreenDelay" value="5000"/> <preference name="ShowSplashScreenSpinner" value="false"/> <preference name="FadeSplashScreen" value="true"/> <preference name="FadeSplashScreenDuration" value="1.0"/> <preference name="SplashScreen" value="screen"/> <preference name="detect-data-types" value="true" /> <preference name="target-device" value="handset" /> <feature name="http://api.phonegap.com/1.0/notification" /> <feature name="http://api.phonegap.com/1.0/geolocation" /> <feature name="http://api.phonegap.com/1.0/network"/> <!-- Whitelist URLs --> <access origin="*" /> <access origin="*.google.com" /> <access origin="*.parse.com" /> <access origin="*.googleapis.com" /> <access origin="*.gstatic.com" /> <access origin="*.googleusercontent.com" /> <access origin="google.com" subdomains="true"/> <access origin="googleapis.com" subdomains="true"/> <access origin="gstatic.com" subdomains="true"/> <access origin="googleusercontent.com" subdomains="true"/> <access origin="parse.com" subdomains="true"/> <allow-navigation href="*" /> <allow-intent href="tel:*" launch-external="yes" /> <allow-intent href="geo:*" launch-external="yes" /> <allow-intent href="*" launch-external="yes" /> <feature name="Device"> <param name="android-package" value="org.apache.cordova.device.Device" /> <param name="ios-package" value="CDVDevice" /> </feature> <feature name="debug-server" required="true"> <param name="domain" value="http://192.168.15.130:8080"/> <param name="key" value="key1"/> </feature> <feature name="Geolocation"> <param name="android-package" value="org.apache.cordova.GeoBroker"/> <param name="ios-package" value="CDVLocation"/> </feature> <feature name="InAppBrowser"> <param name="ios-package" value="CDVInAppBrowser"/> <param name="android-package" value="org.apache.cordova.InAppBrowser"/> </feature> <feature name="SplashScreen"> <param name="android-package" value="org.apache.cordova.SplashScreen"/> <param name="ios-package" value="CDVSplashScreen"/> </feature> <gap:plugin name="com.ionic.keyboard" version="1.0.3"/> <gap:plugin name="uk.co.whiteoctober.cordova.appversion" version="0.1.4"/> <gap:plugin name="org.apache.cordova.splashscreen" version="0.3.4"/> <gap:plugin name="cordova-plugin-inappbrowser" source="npm"/> <gap:plugin name="cordova-plugin-whitelist" source="npm"/> <gap:plugin name="org.apache.cordova.core.geolocation" version="0.3.10"/> <gap:plugin name="com.medlei.pushplugin" version="0.1.2"> <param name="APP_ID" value="Bd8dbfMRwToBaB8UP0SWZ0ZlgYJ2o1CqMtFqzkuh"/> <param name="CLIENT_KEY" value="0ev2Je3JX3ln5XJHTsFTbJwNpG7k5eXpt5l83CVs"/> </gap:plugin> <icon src="resources/icon.png" /> <gap:splash src="resources/splash.png" /> <icon gap:platform="android" src="resources/android/icon/drawable-ldpi-icon.png" gap:density="ldpi"/> <icon gap:platform="android" src="resources/android/icon/drawable-mdpi-icon.png" gap:density="mdpi"/> <icon gap:platform="android" src="resources/android/icon/drawable-hdpi-icon.png" gap:density="hdpi"/> <icon gap:platform="android" src="resources/android/icon/drawable-xhdpi-icon.png" gap:density="xhdpi"/> <icon gap:platform="android" src="resources/android/icon/drawable-xxhdpi-icon.png" gap:density="xxhdpi"/> <icon gap:platform="android" src="resources/android/icon/drawable-xxxhdpi-icon.png" gap:density="xxxhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-ldpi-screen.png" gap:qualifier="land-ldpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-mdpi-screen.png" gap:qualifier="land-mdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-hdpi-screen.png" gap:qualifier="land-hdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-xhdpi-screen.png" gap:qualifier="land-xhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-xxhdpi-screen.png" gap:qualifier="land-xxhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" gap:qualifier="land-xxxhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-ldpi-screen.png" gap:qualifier="port-ldpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-mdpi-screen.png" gap:qualifier="port-mdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-hdpi-screen.png" gap:qualifier="port-hdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-xhdpi-screen.png" gap:qualifier="port-xhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-xxhdpi-screen.png" gap:qualifier="port-xxhdpi"/> <gap:splash gap:platform="android" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" gap:qualifier="port-xxxhdpi"/> <icon gap:platform="ios" src="resources/ios/icon/icon.png" width="57" height="57"/> <icon gap:platform="ios" src="resources/ios/icon/icon@2x.png" width="114" height="114"/> <icon gap:platform="ios" src="resources/ios/icon/icon-40.png" width="40" height="40"/> <icon gap:platform="ios" src="resources/ios/icon/icon-40@2x.png" width="80" height="80"/> <icon gap:platform="ios" src="resources/ios/icon/icon-50.png" width="50" height="50"/> <icon gap:platform="ios" src="resources/ios/icon/icon-50@2x.png" width="100" height="100"/> <icon gap:platform="ios" src="resources/ios/icon/icon-60.png" width="60" height="60"/> <icon gap:platform="ios" src="resources/ios/icon/icon-60@2x.png" width="120" height="120"/> <icon gap:platform="ios" src="resources/ios/icon/icon-60@3x.png" width="180" height="180"/> <icon gap:platform="ios" src="resources/ios/icon/icon-72.png" width="72" height="72"/> <icon gap:platform="ios" src="resources/ios/icon/icon-72@2x.png" width="144" height="144"/> <icon gap:platform="ios" src="resources/ios/icon/icon-76.png" width="76" height="76"/> <icon gap:platform="ios" src="resources/ios/icon/icon-76@2x.png" width="152" height="152"/> <icon gap:platform="ios" src="resources/ios/icon/icon-small.png" width="29" height="29"/> <icon gap:platform="ios" src="resources/ios/icon/icon-small@2x.png" width="58" height="58"/> <icon gap:platform="ios" src="resources/ios/icon/icon-small@3x.png" width="87" height="87"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-568h@2x~iphone.png" height="1136" width="640"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-667h@2x.png" height="1334" width="750"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-736h.png" height="2208" width="1242"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Landscape-736h.png" height="1242" width="2208"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Portrait-736h@3x.png" width="1242" height="2208"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Landscape-736h@3x.png" width="2208" height="1242"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Landscape@2x~ipad.png" height="1536" width="2048"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Landscape~ipad.png" height="768" width="1024"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Portrait@2x~ipad.png" height="2048" width="1536"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default-Portrait~ipad.png" height="1024" width="768"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default@2x~iphone.png" height="960" width="640"/> <gap:splash gap:platform="ios" src="resources/ios/splash/Default~iphone.png" height="480" width="320"/> </widget> 。它们实际上是为了enum的目的而设计的,并且可以根据需要灵活地添加功能。

  

只需查看指向compare constants with enumshere的以下链接以及您将获得的更多链接。我刚发布了一些。

答案 4 :(得分:0)

如果只需要在使用

的类中访问常量
private static void 

甚至只是

static final

会起作用,因为不需要在其他地方调用它。

但是,如果使用private static final作为常量,最好使用getter来调用常量,如下所示:

private static final String CONSTANTVARIABLE = "Some constant";

public String getConstantVariable() {
  return CONSTANTVARIABLE;
}