What is the use of a private static variable in Java? -


If a variable is public static varName; , then I can access it from anywhere ClassName.varName . I also know that stable members are shared with all instances of a class and are not re-specified in each instance.

It is declaring that private static varName; a variable private varName; ?

In both cases it can not be accessed as ClassName.varName or ClassInstance.varName < P>

All can be accessed as ClassName.var_name , but only from within the class that it has been defined - because it is private < / Code> is

public static or private constant variable is often used for constants, for example, many people In the code "defeat E-codes do not like "constant; They want to make a public stable or private static variable with a meaningful name and use it in their code, to make the code more readable. (You can also create such a constant final ).

For example:

  Example of public class {Private final static string JDBC_URL = "jdbc: mysql: // local host / shopdb"; Personal Final Static String JDBC_USERNAME = "User Name"; Personal Final Static String JDBC_PASSWORD = "Password"; Public static zero main (string [] args) {connection conn = DriverManager.getConnection (JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD); // ...}}   

You make it public or personal , depending on whether you want it That they appear outside the variable class or not.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -