It is possible to access and change the values of instance variables from inside a static method.

Topics

  • String Methods
  • Access, Encapsulation, and Static Methods

The public and private keywords

In Java, the keywords public and private define the access of classes, instance variables, constructors, and methods.

private restricts access to only the class that declared the structure, while public allows for access from any class.

Encapsulation

Encapsulation is a technique used to keep implementation details hidden from other classes. Its aim is to create small bundles of logic.

The private Keyword

In Java, instance variables are encapsulated by using the private keyword. This prevents other classes from directly accessing these variables.

public class CheckingAccount{

private String name;

private int balance;

private String id;

}

Accessor Methods

In Java, accessor methods return the value of a private variable. This gives other classes access to that value stored in that variable. without having direct access to the variable itself.

Accessor methods take no parameters and have a return type that matches the type of the variable they are accessing.

public class CheckingAccount{

private int balance;

public int getBalance(){

return this.balance;

}

}

Mutator Methods

In Java, mutator methods reset the value of a private variable. This gives other classes the ability to modify the value stored in that variable without having direct access to the variable itself.

Mutator methods take one parameter whose type matches the type of the variable it is modifying. Mutator methods usually don’t return anything.

public class CheckingAccount{

private int balance;

public void setBalance(int newBalance){

this.balance = newBalance;

}

}

Local Variables

In Java, local variables can only be used within the scope that they were defined in. This scope is often defined by a set of curly brackets. Variables can’t be used outside of those brackets.

public void exampleMethod(int exampleVariable){

}

The this Keyword with Variables

In Java, the this keyword can be used to designate the difference between instance variables and local variables. Variables with this. reference an instance variable.

public class Dog{

public String name;

public void speak(String name){

System.out.println(this.name);

System.out.println(name);

}

}

The this Keyword with Methods

In Java, the this keyword can be used to call methods when writing classes.

public class ExampleClass{

public void exampleMethodOne(){

System.out.println("Hello");

}

public void exampleMethodTwo(){

this.exampleMethodOne();

System.out.println("There");

}

}

The static Keyword

Static methods and variables are declared as static by using the static keyword upon declaration.

public class ATM{

public static int totalMoney = 0;

public static int numATMs = 0;

public static void averageMoney(){

System.out.println(totalMoney / numATMs);

}

Static Methods and Variables

Static methods and variables are associated with the class as a whole, not objects of the class. Both are used by using the name of the class followed by the . operator.

public class ATM{

public static int totalMoney = 0;

public static int numATMs = 0;

public static void averageMoney(){

System.out.println(totalMoney / numATMs);

}

public static void main(String[] args){

System.out.println("Total number of ATMs: " + ATM.numATMs);

ATM.averageMoney();

}

}

Static Methods with Instance Variables

Static methods cannot access or change the values of instance variables.

class ATM{

public static int totalMoney = 0;

public static int numATMs = 0;

public int money = 1;

public static void averageMoney(){

}

}

Methods with Static Variables

Both non-static and static methods can access or change the values of static variables.

class ATM{

public static int totalMoney = 0;

public static int numATMs = 0;

public int money = 1;

public static void staticMethod(){

totalMoney += 1;

}

public void nonStaticMethod(){

totalMoney += 1;

}

}

Static Methods and the this Keyword

Static methods do not have a this reference and are therefore unable to use the class’s instance variables or call non-static methods.

public class DemoClass{

public int demoVariable = 5;

public void demoNonStaticMethod(){

}

public static void demoStaticMethod(){

}

}

Learn More on Codecademy

Can instance variables be changed in static methods?

Static methods cannot access or change the values of instance variables.

Can we access instance variable inside static method?

A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method.

Can you change the value of an instance variable?

With instance variables, each new instance of the class gets a new copy of the instance variables that the class defines. Each instance then can change the values of those instance variables without affecting any other instances.

Can we change value of static variable in C?

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name. Static variables are initialized only once.

Toplist

Neuester Beitrag

Stichworte