2

I'm a beginner in java, and this code to create a public int variable isn't working:

import java.awt.*;
public class Variable_Practice {
    public static void main (String [] args){
       public int number;

It gives me an error on the word number, and it says illegal parameter for modified number; only final is permitted.

There's probably a really simple answer, but I'm a beginner, so sorry if this is a bad question.

3
  • 1
    it's method local.can't modify with public Commented Oct 18, 2014 at 14:43
  • What do you expect to be able to do with that variable? Commented Oct 18, 2014 at 14:43
  • I want to access it in other classes, by extending Variable_Practice. Commented Oct 18, 2014 at 14:46

4 Answers 4

5

Local variables cannot have access modifiers (how would they even make sense?).

There are a few different approaches. Which one you need depends on what you want to do.

If you want a single global variable

public class VariablePractice {
    public static int number;

    public static void main(String[] args) {
    }
}

If you want a variable for each instance of VariablePractice

public class VariablePractice {
    public int number;

    public static void main(String[] args) {
    }
}

If you want a variable for each invocation of main

public class VariablePractice {
    public static void main(String[] args) {
        int number;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

it says i have to wait 10 minutes
It might be worth mentioning that declaring public fields is considered to be bad practice by many software developers.
@Sofffia Where I work, I would not be allowed to check-in code that has a public int foo; If I have a class that makes a value, foo, available to other classes, I have to write an accessor method, public int getFoo() that returns the value. Publishing the value via an accessor gives us more freedom to change the class: Maybe some day we will want to compute foo on-the-fly instead of storing it in a variable. If that ever happens, we only have to change the implementation of the getFoo() method. We don't have to change all of the other code that uses foo.
@jameslarge Maybe you should read here. Robor articulates better than I possibly can why this is a common misconception.
@Sofffia, Ok then, it might be worth mentioning that declaring public fields is wrongly considered to be bad practice by many software developers.
0

public/private/protected is meant at object level and not at method level. You could only use final instead of public in method declaration.

import java.awt.*;
public class Variable_Practice {
public static void main (String [] args){
   final int number = 2;//or something on these lines
   System.out.println(number);
}

Comments

0

You can't make Data Types public inside a method since they run locally inside the method they belong to.

To make the int public you must define it inside your class like so;

 import java.awt.*;
 public class Variable_Practice {
 public int number;

 public static void main (String [] args){

  }
 }

Comments

0

if a variable is declared inside a method, it doesn't require an access specifier. The variable inside a method is called a local variable and you just declare it like int x, double y... As part of your question, if you want to make your variable public, declare it inside your class, but outside the main method.

public class Variable_Practice {
public int number=2;   // this is the place you decelare public variables 
public static void main (String [] args){

   System.out.println(number);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.