0% found this document useful (0 votes)
62 views

Path Menu Get Unstuck Tools: My Home

The document discusses the scope of variables and methods in Java. It explains that variables declared within a method can only be accessed within that method and not outside of it. A variable declared within the class can be accessed by any method in the class. The document uses an example Store class to demonstrate how changing a variable name from within to outside the scope of a method results in an error, showing that the variable is not accessible outside its declared scope.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Path Menu Get Unstuck Tools: My Home

The document discusses the scope of variables and methods in Java. It explains that variables declared within a method can only be accessed within that method and not outside of it. A variable declared within the class can be accessed by any method in the class. The document uses an example Store class to demonstrate how changing a variable name from within to outside the scope of a method results in an error, showing that the variable is not accessible outside its declared scope.

Uploaded by

ilias ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

My Home

Path Menu
Workspace restored.
Get Unstuck
Tools

Learn Java: Methods: Scope

Narrative and Instructions

Learn
LEARN JAVA: METHODS
Scope
A method is a task that an object of a class performs.

We mark the domain of this task using curly braces: {, and }. Everything inside the curly braces
is part of the task. This domain is called the scope of a method.

We can’t access variables that are declared inside a method in code that is outside the scope of
that method.

Looking at the Car class again:

class Car {
  String color;
  int milesDriven;

  public Car(String carColor) {


    color = carColor;
    milesDriven = 0;         
  }

  public void drive() {


     String message = "Miles driven: " + milesDriven;
     System.out.println(message);
  }

  public static void main(String[] args){


     Car myFastCar = new Car("red");
     myFastCar.drive();
  }
}
The variable message, which is declared and initialized inside of drive, cannot be used inside
of main()! It only exists within the scope of the drive() method.

However, milesDriven, which is declared at the top of the class, can be used inside all methods in
the class, since it is in the scope of the whole class.

Instructions

1.
Inside of the advertise() method, change the productType variable to the cookie variable, which
is declared in the main() method. This should also result in the printout:

Selling cookies!
Right?
Checkpoint 2 Passed

Hint
Your advertise() method should now look like:

public void advertise() {


  String message = "Selling " + cookie + "!";
  System.out.println(message);
}
However, you’ll see that this code won’t work, since cookie is not defined in
the advertise() method. That’s okay! It’s helpful to see the error, so that you can better
understand how it looks when you call a variable outside of its scope.
2.
No! We got an error! The cookie variable cannot be accessed inside of the advertise method. The
scope is wrong! Change it back to productType:

String message = "Selling " + productType + "!";


Checkpoint 3 Passed

Hint
Your advertise() method should now look like it did at first:

public void advertise() {


  String message = "Selling " + productType + "!";
  System.out.println(message);
}
3.
Inside of the main() method, print the String message, which is declared in
the advertise() method. This should print:

Selling Cookies!
Right?
Checkpoint 4 Passed

Hint
Your main() method should now look something like:
public static void main(String[] args) {
  String cookie = "Cookies";
  Store cookieShop = new Store(cookie);

  cookieShop.advertise();
  System.out.println(message);
}
However, you’ll see that this code won’t work, since message is not defined in the main() method.
That’s okay! It’s helpful to see the error, so that you can better understand how it looks when
you call a variable outside of its scope.
4.
Foiled again! The message variable only exists inside the scope of the advertise() method!

Delete the faulty print statement from the main() method.


Checkpoint 5 Passed

Hint
Your main() method should now look like it did on at first:

public static void main(String[] args) {


  String cookie = "Cookies";
  Store cookieShop = new Store(cookie);

  cookieShop.advertise();
}
public class Store {

// instance fields

String productType;

// constructor method

public Store(String product) {

productType = product;

// advertise method

public void advertise() {

String message = "Selling " + productType + "!";

System.out.println(message);
}

// main method

public static void main(String[] args) {

String cookie = "Cookies";

Store cookieShop = new Store(cookie);

cookieShop.advertise();

You might also like