Skip to main content
added 15 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

You can read this to understand scope of variables or method  .

In short -:

  1. publicpublic - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. privateprivate - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protectedprotected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. staticstatic - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think of the main method as a place where you can write code to test your class. None ofNo design / logic should be placed in mainmain. 

For example -:

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer to remove all the staticstatic variables used in your class and make those as privateprivate. The two methods getRangegetRange and playGameplayGame can be publicpublic.

You can read this to understand scope of variables or method  .

In short -

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think main method as a place where you can write code to test your class. None of design / logic should be placed in main. For example -

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer remove all the static variables used in your class and make those as private. The two methods getRange and playGame can be public.

You can read this to understand scope of variables or method.

In short:

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think of the main method as a place where you can write code to test your class. No design / logic should be placed in main. 

For example:

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer to remove all the static variables used in your class and make those private. The two methods getRange and playGame can be public.

added 2 characters in body
Source Link
Kinjal
  • 1.1k
  • 2
  • 11
  • 23

You can read this to understand scope of variables or method .

In short -

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think main method as a place where you can write code to test your class. None of design / logic should be placed in main. For example -

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(StringString[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer remove all the static variables used in your class and make those as private. The two methods getRange and playGame can be public.

You can read this to understand scope of variables or method .

In short -

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think main method as a place where you can write code to test your class. None of design / logic should be placed in main. For example -

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

You can read this to understand scope of variables or method .

In short -

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think main method as a place where you can write code to test your class. None of design / logic should be placed in main. For example -

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String[] args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}

I would prefer remove all the static variables used in your class and make those as private. The two methods getRange and playGame can be public.

Source Link
Kinjal
  • 1.1k
  • 2
  • 11
  • 23

You can read this to understand scope of variables or method .

In short -

  1. public - Any member of a class declared as public can be accessed outside of the class. So declare public when you want to give client code access to methods/variables of a class.
  2. private - A private method or field is invisible and inaccessible to other classes, and can be used only within the class in which the field or method is declared.
  3. protected - Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them.
  4. static - In general when a number of objects created from the same class, they all have their distinct copies of instance variables stored in different location. Sometimes we want to have variables that are common to all objects. To accomplish this use static. Read this.

Think main method as a place where you can write code to test your class. None of design / logic should be placed in main. For example -

public class Hilo {
 public Hilo() {}
 //other methods...

public static void main(String args) {
 Hilo hilo = new Hilo();
 hilo.getRange();
 hilo.playGame();
 }
}