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.

bearste1n
  • 23
  • 1
  • 1
  • 3

4 Answers4

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;
    }
}
  • it says i have to wait 10 minutes – bearste1n Oct 18 '14 at 14:47
  • It might be worth mentioning that declaring `public` fields is considered to be bad practice by many software developers. – Solomon Slow Oct 18 '14 at 16:03
  • @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. – Solomon Slow Oct 18 '14 at 16:13
  • @jameslarge Maybe you should [read here](http://stackoverflow.com/a/12108025/493122). Robor articulates better than I possibly can why this is a common misconception. – Shoe Oct 18 '14 at 16:18
  • 1
    @Sofffia, Ok then, it might be worth mentioning that declaring `public` fields is _wrongly_ considered to be bad practice by many software developers. – Solomon Slow Oct 18 '14 at 16:23
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);
}
SMA
  • 36,381
  • 8
  • 49
  • 73
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){

  }
 }
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);
}
Binalfew
  • 1
  • 1