Android Studio is designed mainly for writing Java for Android apps. However, you can use it to write generic Java code.
We will set up the Android Studio environment and perform some simply test.
package com.example; public class Tutorial { public static void main(String[] args) { } }
CODE EXPLANATION: - The com.example reprsents the package - The key word public class {...} defines the class definition - The public static vodid main(){...} represents the main() method declaration. It is used in this tutorial
to output result in the Run panel. It is NOT used when creating Android Studio apps.
package com.example; public class Tutorial { public static void main(String[] args) { System.out.println("Hello, World"); } }
package com.example; public class Tutorial { public static void main(String[] args) {
String myCustomMessage = "This is my custom message."; System.out.println("Hello, World"); System.out.println(myCustomMessage); } }
We will create a class and its getters and setters. Remember, classes are blueprints (or templates) for an object. They are NOUNS within an app.
Fields are used to define what are called field variables. These fields are called properties when creating an object (e.g, myObject.property) using the dot.syntax. To determine what fields are needed, ask yourself the question, "What would be somethings that an object would have?" In the case of a Player class, we used playerName, score, level, and lives. Notice that lives are plural because a player could have more than one life.
public class Player { String playerName;
int score;
int level;
int lives; }
Now that we have fields defined, we can create a constructor which is used to INTIALIZE the DEFAULT values of those class variables (e.g., set playerName = "unknown", score=0, level=1, and lives=3) which can be overwritten later.
public class Player { String playerName;
int score;
int level;
int lives;
public Player(){
playerName = "Not yet defined";
score = 0;
level = 1;
lives = 9;
} }
Now that we have a class (a blueprint), we can now create an object from it.
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player();
System.out.println("What is player name? " + Cornelius.playerName);
System.out.println("What is game score? " + Cornelius.score);
System.out.println("What is game level? " + Cornelius.level);
System.out.println("How many lives does the player have? " + Cornelius.lives);} }
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player();
System.out.println("What is player name? " + Cornelius.playerName);
System.out.println("What is game score? " + Cornelius.score);
System.out.println("What is game level? " + Cornelius.level);
System.out.println("How many lives does the player have? " + Cornelius.lives);} Cornelius.playerName = "Cornelius";
System.out.println("What is player name? " + Cornelius.playerName); }
CODE EXPLANATION: - The constructor is used to set the INITIAL VALUE(S) of a class. However, you can change those initial values at any time after the object is created through properties. However, using the above approach is not a good programming practice to allow external code to ACCESS the internal working of a class DIRECTLY.
A better approach of changing the value of a class is to set its fields to private and then use a concept of getters and setters to access or change those values.
public class Player { private String playerName;
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not yet defined";
score = 0;
level = 1;
lives = 9;
} }
CODE EXPLANATION:
- The private keywords are used to limit access to the fields only from WITHIN its class but not OUTSIDE of its class.
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player(); }
To provide access outside of a class after you have made the fields private, you have to create what is called getters and setters.
public class Player { private String playerName;
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not yet defined";
score = 0;
level = 1;
lives = 9;
} public String getPlayerName(){
return playerName;
}
public void setPlayerName(String player) {
playerName = player;
}
}
CODE EXPLANATION:
- By convention, a getter is prefixed with the word "get" and always has the return keyword of the field that is private. It will GET and RETURN the current value when accessed from outside code.
- By convention, a setter is prefixed with the word "set" and the field value is SET to the argument value that will be passed into it from the outside code. It may have the word return (See next series of steps).
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player(); Cornelius.setPlayerName("Cornelius");
System.out.println(Cornelius.getPlayerName()); }
public class Player { private String playerName;
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not Defined Yet";
score = 0;
level = 1;
lives = 9;
} public String getPlayerName(){
return playerName;
}
public void setPlayerName(String player) {
if(player.length() < 4)
{
return;
}
else
{
playerName = player;
}
}
}
CODE EXPLANATION:
- If the player name is less than four then the code is returned with the default value ("Not yet defined"); otherwise, the code is set to the new value.
- For experience developer, you could have written a shorten version of the "if" statement:
if(player.length() < 4)
returns;
playerName = player;
Cornelius.setPlayerName("Co");
The previous constructor is called a no argument constructor because as the name implies it has no argument. However, you can create MULTIPLE constructors within the SAME class and have the outside code dictate which constructor to use based on if it has no argument or the set number or arguments. This process is called overloading a constructor. The no argument constructor will get you DEFAULT values; whereas, the overloaded constructor can allow you to set one or all of the fields (class variables) to DIFFERENT VALUES. The constructor name and its parameters is called the constructor mehtod SIGNATURE.
public class Player { private String playerName;
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not Defined Yet";
score = 0;
level = 1;
lives = 9;
}
public Player(String player){
setPlayerName(player);
score = 0;
level = 1;
lives = 9;
}
public String getPlayerName(){
return playerName;
}
public void setPlayerName(String player) {
if(player.length() < 4)
{
return;
}
else
{
playerName = player;
}
}
}
CODE EXPLANATION:
- It is important to note that the second constructor has one property that is set OUTSIDE of the class (e.g., (setPlayerName(player);) and three properties that are set to INITIAL VALUES (e.g., score = 0, level = 1; and lives = 9).
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player(); Cornelius.setPlayerName("Cornelius");
System.out.println(Cornelius.getPlayerName());
Player Jones = new Player ("Jones");
System.out.println(Jones.getPlayerName());
}
CODE EXPLANATION:
- Unlike the first object where the setPlayerName() method had to be used to set the player's name, the second object player's name was set at the SAME TIME the object was created by passing it INTO the constructor as an argument. This is a better approach to use then the no argument constructor because it eliminate the need to use setPlayerName method AFTER the object is created instead of WHEN the object is created.
While you could write a getter and setter for each field, it can become very tedious and error prone if you have to write a lot of them. To save you time, you can let Android Studio create them for you AUTOMATICALLY based on the fields in the class:
public class Player { private String playerName;CODE EXPLANATION:
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not Defined Yet";
score = 0;
level = 1;
lives = 9;
}
public Player(String player){
setPlayerName(player);
score = 0;
level = 1;
lives = 9;
}
public String getPlayerName(){
return playerName;
}
public void setPlayerName(String player) {
if(player.length() < 4)
{
return;
}
else
{
playerName = player;
}
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = lives;
}
}
private int level ... public void setLevel(int level) {- So it is best practice to use the word "this" to make it clear that "this" refers to the class variable at the top of the class. It is also best practice to use the word "this" is a constructor as well.
this.level = level;
}
public class Player { private String playerName;
private int score;
private int level;
private int lives;
public Player(){
playerName = "Not Defined Yet";
score = 0;
level = 1;
lives = 9;
}
public Player(String player, int score, int level, int lives){
setPlayerName(player);
setScore(score);
setLevel(level);
setLives(lives);
}
public String getPlayerName(){
return playerName;
}
public void setPlayerName(String player) {
if(player.length() < 4)
{
return;
}
else
{
playerName = player;
}
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getLives() {
return lives;
}
public void setLives(int lives) {
this.lives = lives;
}
}
package com.example; public class Tutorial { public static void main(String[] args) {
Player Cornelius = new Player(); Cornelius.setPlayerName("Cornelius");
System.out.println(Cornelius.getPlayerName());
Player Jones = new Player ("Jones", 2, 2, 2);
System.out.println(Jones.getPlayerName());
System.out.println(Jones.getScore());
System.out.println(Jones.getLevel());
System.out.println(Jones.getLives());
}