Calling Non-Static Method In Static Method In Java
The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.
Static methods cannot call non static methods directly
According to the restrictions written in javatpoint, static methods cannot call non-static methods directly.
Sure they can. They just need an instance on which to call them. Here's an example of a static method directly calling a non-static method:
class Example {
public static void main(String[] args) {
Example e = new Example();
e.hiThere(); // <========== Direct call to instance method
go(e);
}
private static void go(Example e) {
e.hiThere(); // <========== Direct call to instance method
}
private void hiThere() {
System.out.println("Hi there");
}
}
What exactly is it meant by 'directly' and why cant i do that?
They probably mean "not using an instance." It's not how I would define "directly" at all. For instance, in the example above, main
cannot call hiThere
without having an instance to call it on. But once it has an instance (in main
's case, by creating one), it can directly call hiThere
on it. Also in the example, go
can directly call hiThere
; it receives an instance as an parameter.
And yeah what are the indirect ways in which i can do it?
They aren't indirect: You need an instance on which to call the method. But when a static method has an instance, calling the instance method is as direct as any other method call.
Call non-static method in Static method JAVA
It doesn't look like making sense to declare callBackPhoto
as a static
method. If you have put static
keyword accidentally in its declaration, simply remove it to solve your problem i.e. replace
public static void callBackPhoto( List<Photo> photos, JSONArray items)
with
public void callBackPhoto( List<Photo> photos, JSONArray items)
Note that there is no way to call a non-static method from a static
one directly (i.e. without calling it on an object/instance). Thus, if for any reason, you can't remove static
keyword from the declaration of callBackPhoto
, you are left with only two options:
Declare
build
too asstatic
Call
build
on an object/instance e.g.new PhotosActivity().build()
Though any of these two options will solve your problem, I don't think this is how you intend to design your class.
How to call a non-static method from a static method
The only way to call a non-static method from a static method is you should have
an instance of the class containing the non-static method.
Like in your question :
CheckConnection checkInternet = new CheckConnection();
if (checkInternet.isNetworkAvailable()) {
// Has internet Connection
} else {
// No Internet Connection
You have instance of CheckConnection
so you can call it.
So there is no problem in your code with non-static method from a static method may be some other thing is responsible for application freezes.
Calling non-static method in static method in Java (Non-Static Variable Error)
You trying to use inner class in static context as static nested. So you should chenge it so static nested:
public static class sclass
or you can use it throw referance to upper class like this:
UpperClassName upperClass = new UpperClassName();
sclass ss = upperClass.new sclass();
Docs about nested classes.
Java conventions for calling non-static methods from the main() method
That's exactly how.
Create an object instance. Call the methods on the instance.
For example, this is a typical code in a Springboot application.
An instance of SpringApplication
is created and then the instance method run
is invoked.
public class Application {
public static void main(final String[] args) {
final SpringApplication application =
new SpringApplication(Application.class);
application.run(args);
}
}
As for the name of the "main" method, in the example above run
makes sense because you want to run
the application.
But it could be anything like run
, execute
, start
, serve
, scan
it depends on what your program does.
In your specific case I would call it calculateFibonacci
, or if your class was named Fibonnacci then just calculate()
Is this how you would do it or what should I change?
I think your code looks good.
The way I would've written would be as follows
public class Fibonacci {
public static void main(String[] args) {
Fibonacci fibonacci = new Fibonacci();
fibonacci.execute();
}
public void execute(){
System.out.println(fibonacci(10));
}
public int fibonacci(int n){
if (n <= 0){
throw new IllegalArgumentException(
"n must be greater then 0. Received: " + n);
}
if (n == 1 || n == 2){
return 1;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}
}
The reason for the exception instead of null it to indicate that's an actual invalid (or illegal) argument. The program can't calculate, whereas null
would indicate the computation for a negative number results in null
which is not the case.
But the way you have it is completely fine.
Related Topics
How to Add Jtable in Jpanel With Null Layout
What's the Simplest Way to Print a Java Array
What Is a Stack Trace, and How to Use It to Debug My Application Errors
How to Upload Files to a Server Using Jsp/Servlet
How to Generate Random Integers Within a Specific Range in Java
What Issues Should Be Considered When Overriding Equals and Hashcode in Java
Why Am I Getting a Noclassdeffounderror in Java
Eclipse/Tomcat: Deploy Doesn't Work Any More (Classnotfoundexception)
Rjava Load Error in Rstudio/R After "Upgrading" to Osx Yosemite
Selenium: How to Stop Geckodriver Process Impacting Pc Memory, Without Calling Driver.Quit()
Is Java "Pass-By-Reference" or "Pass-By-Value"
The Use of Multiple Jframes: Good or Bad Practice
What Is the Recommended Way to Escape HTML Symbols in Plain Java
How to Upload a File Using Java Httpclient Library Working With PHP
Android, Getting Resource Id from String
Are There C++ Equivalents For the Protocol Buffers Delimited I/O Functions in Java