6 Replies - 387 Views - Last Post: 25 June 2013 - 12:14 PM Rate Topic: -----

#1 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Local variable to non public variable

Posted 23 June 2013 - 01:06 PM

I was wondering how I would make a local variable, for an if else statement, public. For example, this part of my code I want to create the local variable(s), FOOD or REDO, accessible outside of the block.


if(yesOrNo.equalsIgnoreCase(yes)){
		System.out.println("List the items for " + list);
		String food = input.nextLine();
                System.out.println("You chose " + food);
		// bring answer outside of block
		
		
		}
		
		else{
		System.out.println("Please re-enter the list you would like to go over");
		String redo = input.nextLine();
		System.out.println("You chose " + redo);
		// bring answer outside of block
		}


This post has been edited by streek405: 23 June 2013 - 01:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Local variable to non public variable

#2 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 138
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Local variable to non public variable

Posted 23 June 2013 - 01:10 PM

Declare the variables outside the blocks then set the variable equal to the input in the blocks and you won't have a problem.

i.e.

import java.util.*;

public class Test {
    
    Scanner scanner = new Scanner(System.in);
    
    int count = 0;
    
    Test() {
        System.out.println("Enter variable");
        try {
            count = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("Not a number, retry:");
            count = Integer.parseInt(scanner.nextLine());
        }
        if(count > 5) 
            System.out.println("Count is high: " + count);
        else
            System.out.println("Count is low: " + count);
    }
    
    public static void main(String[] arguments) {
        Test test = new Test();
    }
    
}


This post has been edited by schutzzz: 23 June 2013 - 01:21 PM

Was This Post Helpful? 1
  • +
  • -

#3 skuz  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 6
  • Joined: 13-June 13

Re: Local variable to non public variable

Posted 23 June 2013 - 01:14 PM

All you need to do is change the where the variable is declared in your program to broaden its scope. If you declare the variable outside of the if statement, you'll be able to access it both inside and outside of the if statement, like so:


// The variable is declared outside the if statement.
String name = "Johnny"

if (name.equals("Johnny")) {
	// Because the variable was declared above, it can be accessed in the if statement. This is a concept called scope.
	System.out.print("Hi " + name);
	// You can access the variable like right above, and you can also modify it, like so:
	name = "Timmy";
}
else {
	System.out.print("We don't like your type around here");
}




Here's an explanation on scope, which may help you: http://www.java-made...able-scope.html
Was This Post Helpful? 1
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Local variable to non public variable

Posted 23 June 2013 - 04:14 PM

Global Instance variable = A variable which is accessible for the life of an instance of a class.
Public variable = A variable which can be accessed from any other class in the project.

There's an imperative difference between access modifiers and variable scope.

Just wanted to clear that up.

This post has been edited by Flukeshot: 23 June 2013 - 04:33 PM

Was This Post Helpful? 1
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1428
  • View blog
  • Posts: 3,172
  • Joined: 05-April 11

Re: Local variable to non public variable

Posted 23 June 2013 - 04:25 PM

View PostFlukeshot, on 23 June 2013 - 11:14 PM, said:

Global variable = A variable which is accessible for the life of an instance of a class.
Public variable = A variable which can be accessed from any other class in the project.

There's an imperative difference between access modifiers and variable scope.

Just wanted to clear that up.

Be careful with using the global term since Java doesn't have global variables
You are able to have static variables which kinda act like global variables, but in the scope of a class
A static variable is not only accessible for the life of an instance of a class
The static variable lives even after all of the instances of the class has been garbage collected. It is not coupled to an instance, but to the class
Was This Post Helpful? 1
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Local variable to non public variable

Posted 23 June 2013 - 04:34 PM

Of course, my mistake. Fixed previous post.
Was This Post Helpful? 0
  • +
  • -

#7 streek405  Icon User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 610
  • Joined: 10-March 13

Re: Local variable to non public variable

Posted 25 June 2013 - 12:14 PM

View Postskuz, on 23 June 2013 - 01:14 PM, said:

All you need to do is change the where the variable is declared in your program to broaden its scope. If you declare the variable outside of the if statement, you'll be able to access it both inside and outside of the if statement, like so:


// The variable is declared outside the if statement.
String name = "Johnny"

if (name.equals("Johnny")) {
	// Because the variable was declared above, it can be accessed in the if statement. This is a concept called scope.
	System.out.print("Hi " + name);
	// You can access the variable like right above, and you can also modify it, like so:
	name = "Timmy";
}
else {
	System.out.print("We don't like your type around here");
}



Ok, thanks!

Here's an explanation on scope, which may help you: http://www.java-made...able-scope.html


View Postschutzzz, on 23 June 2013 - 01:10 PM, said:

Declare the variables outside the blocks then set the variable equal to the input in the blocks and you won't have a problem.

i.e.

import java.util.*;

public class Test {
    
    Scanner scanner = new Scanner(System.in);
    
    int count = 0;
    
    Test() {
        System.out.println("Enter variable");
        try {
            count = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("Not a number, retry:");
            count = Integer.parseInt(scanner.nextLine());
        }
        if(count > 5) 
            System.out.println("Count is high: " + count);
        else
            System.out.println("Count is low: " + count);
    }
    
    public static void main(String[] arguments) {
        Test test = new Test();
    }
    
}


Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1