0

Newbie to Java here. I'm in the process of porting my iPhone app to Android. From what I've read, the final keyword is pretty much equivalent to static. But does it work the same inside a method?

For example in Objective-C inside a method... static Class aClass = [[aClass alloc] init]; wouldn't be reallocated again and it wouldn't be disposed at the end of the method.

Would something in Java inside a method like... final Class aClass = new aClass(); act the same?

Scott
  • 1,154
  • 1
  • 12
  • 25
  • 9
    `From what I've read, the final keyword is pretty much equivalent to static` : you should throw away your Java book, `final` and `static` are like sun and moon – Dici Jan 04 '15 at 05:33
  • You'll find various semantics of `final` in Java in this post http://stackoverflow.com/questions/26027027/java-changing-value-of-final-array-element/26027048#26027048 – Dici Jan 04 '15 at 05:35
  • @Dici Thanks, I guess it's less confusing that way. – Scott Jan 04 '15 at 05:36
  • Sorry, it was the wrong post, I actually thought of this one http://stackoverflow.com/questions/27348577/is-there-an-advantage-to-declaring-a-private-method-final-in-java/27348639#27348639 – Dici Jan 04 '15 at 05:38
  • 2
    `final` is to `static` what potatoes are to doorways. They are *completely* different things. – user253751 Jan 04 '15 at 06:34

4 Answers4

4

No. Block-local variables go out of scope when the block is exited and are logically (and usually physically) allocated on the stack.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

Java isn't really like Objective C in that respect, and final is more akin to const because it indicates a reference may not be altered. In your example, when the block ends the final variable will be eligible for garbage-collection as it is no longer reachable. I think you want a field variable something like

static final aClass aField = new aClass();

Note that Java class names start with a capital letter by convention...

static final MyClass aField = new MyClass();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Oops, that was just an oversight. I didn't include the class at all. Edited. Is there any keyword that would store them or do I just need to store it in file scope? – Scott Jan 04 '15 at 05:39
  • 1
    There is no equivalent keyword, you need them in the class scope (so it is a field). – Elliott Frisch Jan 04 '15 at 05:40
1

You are confusing the meaning of Final and Static.

Final means that the value of the variable cannot be changed after its value is initially declared.

Static means a variable can be accessed and changed without needing to instantiate a class beforehand.

Perhaps the following bit of code will make this more clear.

public class SF1 {

static int x;
final int y = 3;
static final int z = 5;

public static void main(String[] args) {
        x = 1;
        // works fine

        SF1 classInstance = new SF1();
        classInstance.y = 4;
        // will give an error: "The final field main.y cannot be assigned"

        z = 6;
        // will give an error: "The final field main.z cannot be assigned"

        reassignIntValue();
        // x now equals 25, even if called from the main method

        System.out.println(x);
    }

    public static void reassignIntValue() {
        x = 25;
    }
}
Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
0

You have to declare your variable in class scope so you can able to access it outside of your method.

class ABC{

 int a;

 public void method(){
    a = 10; // initialize your variable or do any operation you want.  
 }


 public static void main(String args[]){

    ABC abc = new ABC();
    System.out.println(abc.a) // it will print a result
 }     

}
atish shimpi
  • 4,873
  • 2
  • 32
  • 50