Asked  6 Months ago    Answers:  5   Viewed   39 times

See the code snippets below:

Code 1

public class A {
    static int add(int i, int j) {
        return(i + j);
    }
}

public class B extends A {
    public static void main(String args[]) {
        short s = 9;
        System.out.println(add(s, 6));
    }
}

Code 2

public class A {
    int add(int i, int j) {
        return(i + j);
    }
}

public class B extends A {
    public static void main(String args[]) {
    A a = new A();
        short s = 9;
        System.out.println(a.add(s, 6));
    }
}

What is the difference between these code snippets? Both output 15 as an answer.

 Answers

98

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

Example:

class Foo {
    int i;

    public Foo(int i) { 
       this.i = i;
    }

    public static String method1() {
       return "An example string that doesn't depend on i (an instance variable)";
    }

    public int method2() {
       return this.i + 1; // Depends on i
    }
}

You can call static methods like this: Foo.method1(). If you try that with method2, it will fail. But this will work: Foo bar = new Foo(1); bar.method2();

Tuesday, June 1, 2021
 
seaders
answered 6 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
84

Services are given higher priority than Activitys at the process-level. When memory is low, the Android system will prioritize Services over Activitys, making Services the ideal option for long-running tasks. See the article titled Processes and Threads for more information.

Also, when you state the following in your original post:

Fragments can be used without defining a user interface. It is recommended to use headless fragments for your background processing.

Where are you quoting this from? I agree with the first sentence, but the second sentence is too general. For short-running tasks (such as performing HTTP requests, etc.), headless fragments work fine. However, for performing long-running background processing (such as downloading a very large file, etc.) a headless fragment may not be what you want. For example, if you used a headless fragment to perform a long-running task and the user clicked the "back button", this will cause both the Activity and its headless Fragment to be destroyed.

To summarize, a service is a background component that exists independent of an Activity, meaning that it can continue to run in the background even if the Activity which started the service is destroyed. On the other hand, a headless fragment will always have an associated parent Activity. If the Activity that hosts the fragment is destroyed by the system, then the fragment will have to be killed as well.

Monday, August 9, 2021
 
Dave B
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
36

The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time.

const static int x = rand();
Wednesday, November 3, 2021
 
rob mayoff
answered 4 Weeks 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