Here I am using another GUI/IDE, Eclipse, along with handy command line
sdk.
The features to be noted
|
The example code
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hi");
process("hi eclipse calling from static");
test tt = new test();
tt.method_2("call through an object");
}
//static void process(String str)
void process(String str)
{
System.out.println(str);
}
void method_2(String str)
{
System.out.println(str);
}
}
|
 |
If you note two arrows and the word underlined with a squiggly line,
show an error that generated when a static method tries to call a non-static
method . You could have called this method via object, but a static member
method can be called by method's name like process().
 |
|
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
Cannot make a static reference to the non-static method process(String) from
the type test
at test.main(test.java:10)
|
 |
at the completion of run |
 |
|
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hi");
process("hi eclipse calling from static");
test tt = new test();
tt.method_2("call through an object");
}
static void process(String str)
{
System.out.println(str);
}
void method_2(String str)
{
System.out.println(str);
}
}
|
However, other methods can be used via instances of the class, created
in the client method. In the example below, method_2 was used via the
tt.method_2("a string"); |
 |
Complete Code :
import java.io.*;
import javax.swing.JOptionPane;
import java.net.URI;
public class test {
public test() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args)
throws Exception {
// TODO Auto-generated method stub
test tt = new test();
try {
//
System.out.println("Hi");
process("hi eclipse calling from static");
tt.method_2("call through an object");
//
} catch (NumberFormatException exc) {
//
}
}
static void process(String str)
{
System.out.println(str);
}
void method_2(String str)
{
System.out.println(str);
}
}// end of class template
|
|
|
|