Asked  6 Months ago    Answers:  5   Viewed   26 times

I just started coding. I want to use a switch statement twice for the same variable, and I was told that to do this the variable would have to be 'in scope'.

Being a beginner, I have no idea what that means. So what does being in scope mean? And, if a variable isn't in scope, how do I make it in scope?

 Answers

31

A local variable1 is "in scope" if code can access it and out of scope if it can't. In Java, variables are scoped to the block ({}) they're declared in. So:

void foo() {
    int a = 42;

    if (/*some condition*/) {
        String q = "Life, the Universe, and Everything";

        // 1. Both `a` and `q` are in scope here
        System.out.println(a);
        System.out.println(q);
        if (/*another condition*/) {
            // 2. Both `a` and `q` are in scope here, too
            System.out.println(a);
            System.out.println(q);
        }
    }

    // 3. Only `a` is in scope here
    System.out.println(a);
    System.out.println(q); // ERROR, `q` is not in scope
}

Note (1), (2), and (3) above:

  1. The code can access q because q is declared in the same block as the code; tt can access a because it's declared in the containing block.

  2. The code can access q because it's declared in the containing block; it can access a because it's in the next block out.

  3. The code can access a, but not q, because q isn't declared in the block or any of the blocks (or a couple of other things) containing it.

When figuring out what an unqualified identifier (like a or q above, as opposed to the foo in this.foo or the toLowerCase in q.toLowerCase, which are qualified) is, the Java compiler will look in each of these places, one after the other, until it finds a match:

  • For a variable with that name in the innermost block
  • For a variable with that name in the next block out, and so on
  • For a field2 or method (generally: member) with that name in the current class
  • For a class with that name from a package that's been imported
  • For a package with that name

There are a few others for that list (I'm not going to get into static imports with a beginner).

There's a lot more to scope, I suggest working through some tutorials and/or a beginning Java book for more.


1 "local variable" vs. "variable" - The Java Language Specification uses "variable" in a more general way than most people do in common speech. When I say "variable" in this answer, I mean what the JLS calls a "local variable".

2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)

Tuesday, June 1, 2021
 
ajreal
answered 6 Months ago
50

[[Scopes]] is a private property that Chrome developer tools add and use internally, in C++, here in the source. It displays the variables that are in the scope of a function, i.e. which variables can be accessed from that function.

For example:

function a() {
  var foo = 'foo';
  var obj = {
    bar: function () {
      return foo;
    }
  };
  console.log(obj);
}
a();

Here, the function that is attached to property obj.bar has variable foo in its scope, so when we inspect the [[Scopes]] propety of obj.bar we see something like

[[Scopes]]: Scopes[2]
0: Closure (a)
  foo: "foo"
1: Global
  (all global variables)

You can manually inspect these properties in the console, which might be helpful for debugging, but you can't access them using JavaScript and you shouldn't care about them in your application code.

See also: SO - Access function location programmatically.

Thursday, July 29, 2021
 
WooDzu
answered 4 Months ago
17

There is an issue already filed about this at the OpenJFX docs.

While it hasn't been resolved yet, there is a possible workaround, based on:

NetBeans only adds javadoc/source jars for a jar with the exact same name and -javadoc/-source suffix

So here are the steps to solve it:

  • Install NetBeans 10 and JDK 11.0.2.

  • Clone the HelloFX sample for NetBeans and Maven, from the OpenJFX samples.

  • Update the JavaFX dependencies to 11.0.2.

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
    
  • Run it:

    mvn clean compile exec:java
    
  • Check that the JavaFX dependencies have been downloaded to your local m2 repository. Under <user home>/.m2/repository/org/openjfx/javafx-base/11.0.2 for instance you will find javafx-base-11.0.2.jar and javafx-base-mac-11.0.2.jar (or win, or linux based on your platform).

  • Back on NetBeans, right click in the Dependencies folder and select Download Sources (see the task progress in the bottom right taskbar), and then Download Javadoc(see the task progress).

  • Go to your m2 repository and verify that there are now -source and -javadoc jar files.

However, this won't solve the issue yet, there is an extra step:

  • In your m2 repository, manually rename the -source and -javadoc jar files using your platform classifier, to -mac-source and -mac-javadoc (or win, or linux based on your platform). Do this for the different JavaFX modules:

Back to NetBeans, check that now you have JavaDoc, or if you press Ctrl/CMD+Click you can access the source.

Note that this fix has to be done only once, the rest of your Maven projects should pick JavaDoc and Sources.

Thursday, August 5, 2021
 
spender
answered 4 Months ago
36

StampedLock is an alternative to using a ReadWriteLock (implemented by ReentrantReadWriteLock). The main differences between StampedLock and ReentrantReadWriteLock are that:

  • StampedLocks allow optimistic locking for read operations
  • ReentrantLocks are reentrant (StampedLocks are not)

So if you have a scenario where you have contention (otherwise you may as well use synchronized or a simple Lock) and more readers than writers, using a StampedLock can significantly improve performance.

However you should measure the performance based on your specific use case before jumping to conclusions.

Heinz Kabutz has written about StampedLocks in his newsletter and he also made a presentation about performance.

Sunday, August 8, 2021
 
capsid
answered 4 Months ago
68

if you are going to step down, then change your project's source to 1.7 as well,

right click on your Project -> Properties -> Sources window 

and set 1.7 here

note: however I would suggest you to figure out why it doesn't work on 1.8

Saturday, September 4, 2021
 
Kenny
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share