View Single Post
Skilled contributor
10Jul2011,20:59  
ManzZup's Avatar
then donot make them static because static means they are not instance related but it is related to the class
i think this would be something you are looking for

Code:
public class Reuser{
      public int someNum = 10;
      private String name;
      public double sum;
     
      //you dont have static ones anymore because static ones are not changedd with instance
     //you can use both public and private and the difference is that you cannot directly call the private ones, i'll show it in the other code block

      public Reuser(String str){
             name = str;
      }
      //This is a class constructor, as i cannot directly assign value to the private variable, i use the constructor to do that and even you can use a setter method to do this
      public void setName(String str){
           name = str;
      }
   
      //To get the value of the method you use a getter
      public String getName(){
            return name;
      } 
 }
A class that uses multiple of this

Code:
class User{
        public static void main(String... args){
                Reuser user1 = new Reuser("Giving the name");
                Reuser user2 = new Reuser("You always have to set this as there's no defualt constructor now");
                user1.setName("Changing the name");
                System.out.println(user1.someNum);  //calling a public variable no prob;
                user1.sum = 1221.31361d; //setting a public variable so no prob
                System.out.println(user1.sum);

                //The below code IS COMMENTED BECAUSE IT IS WRONG
                // user1.name = "You cannot access private variable like this";
               // System.out.println("Not even like this :" + user1.name);

                System.out.println(user1.getName()); //you can use the getter method to recieve the value
 
        }
}
I hpe you get that, but please tell me for any erros in the code because i didnt compile the code beforehand
askmurphy like this