Java Overloaded Constructors [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding which constructor is chosen and why
Why compiler acts like this,
public class Calculator{
private Calculator(Object o) {
// code goes here
}
private Calculator(double[] calc) {
// code goes here
}
public static void main(String[] args) {
new Calculator(null);
}
}
This program executes second constructor. Why first constructor not execute?

Both constructors are accessible and applicable.
The constructor Calculator (Object) accepts any parameter passed to Calculator (double[]), so Calculator (Object) is less specific.

Related

java static method why is needed [duplicate]

This question already has answers here:
"Non-static method cannot be referenced from a static context" error
(4 answers)
Closed 6 years ago.
I'm using eclipse as IDE for java.
I wrote the following code, but I have one error on loadStrade(). Eclipse suggested me to change loadStrade from public void to public static, and I don't understand why?
I've looked for similar problem and I've found some problems like mine, but I still not understand why I have to change method to static. Uffa!
In the code, routesNet is a graph (jgraphT), and loadStrade() is used to populate vertex and edge.
Can I have help. Thanks, Fabrizio
public class GestioneStrade {
private Stradario routesNet;
public static void main(String[] args) {
/*
* Instantiate Stradario and fill it with routes and cross
*
*/
GestioneStrade m = new GestioneStrade(); //istance of gestionestrade ok?
// now I set new routesNet
m.setRoutesNet(new Stradario());
loadStrade(m.getRoutesNet()); // why loadStrade must be static :-(
}
public Stradario getRoutesNet() {
return routesNet;
}
public void setRoutesNet(Stradario routesNet) {
this.routesNet = routesNet;
}
public void loadStrade(Stradario str) {
// some code to fill routesNet
}
In "main" you should replace
loadStrade(m.getRoutesNet());
to
m.loadStrade(m.getRoutesNet());
And leave loadStrade as non-static.

How to manipulate a Variable from another class using a method call from that other class? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
public class Main{
.
.
.
Class class = new Class();
class.method(class.variableFromAnotherClass);
sysout(class.variableFromAnotherClass)
}
public class Class{
Int variableFromAnotherClass=0;
method(int var){
var=1;
}
}
Output:
0
Im dealing with a situation similar to the one above except with more variables in my version of "Class". My issue is that the method within "Class" does nothing to the variable being passed through. Why does this happen? Is it because the variable being passed through already belongs to the class? Any suggestions on What should I do to solve this problem?
The problem you are facing has to do with Java passing variables by reference, instead of by value. when variableFromAnotherClass is passed into class.method(int var), the int var variable is created from a copy of variableFromAnotherClass. so when var=1 is called, it updates the copy of variableFromAnotherClass, and not variableFromAnotherClass itself.
If you want to be able to change variableFromAnotherClass from Main, you can write class.variableFromAnotherClass=1; instead of calling method.
Another, more professional way of updating variables is using getters and setters:
public class Class {
int variableFromAnotherClass;
public void getVar() {
return variableFromAnotherClass;
}
public int setVar(int var) {
this.variableFromAnotherClass = var;
}
}
please make both the method and the variable static
public class Class{
static Int variableFromAnotherClass=0;
static method(int var){
var=1;
}
}
Try java getters and setter and make instance variables of another class private which will add encapsulation.

Static method getting called using a null reference [duplicate]

This question already has answers here:
Why I am not getting NullPointerException? [duplicate]
(6 answers)
Closed 6 years ago.
public class Test {
public static void main(String[] args) {
Test test = null;
test.func();
}
static void func(){
System.out.println("Hello!!");
}
}
Why this program is getting executed successfully?
This is because static methods are not related to instances. Compiler internally convert this and call Test.func()
You call a static method. Static methods are invoked on Classes.
The call is like this Test.func().
Tip: In your code when you call static methods. Call them Class.method() not on object.

Java - Method picking algorithm [duplicate]

This question already has answers here:
Using null in overloaded methods in Java [duplicate]
(3 answers)
Closed 7 years ago.
I was messing around with methods and was looking, which Method will be executed if I make two Methods named "hello", with different objects they want and pass a "null" to the method:
public static void main(String... args) {
hello(null);
}
public static void hello(Window w) {
System.out.println("Hello");
}
public static void hello(Frame f) {
System.out.println("Bye");
}
The output was every time "Bye", but I still don't understand the logic behind that.
After a short research with google, without of any explanation, I decided to ask the question here.
I hope that someone can explain the selection algorithm or give me a link to a explanation.
The compiler prefers the most specialized type:
public class Parent {
public static void main(String... args) {
hello(null); // takes `Child`
}
public static void hello(Parent _) {
System.out.println("SuperClass");
}
public static void hello(Child _) {
System.out.println("SubClass");
}
}
class Child extends Parent {
}
For the reason, take a look at this thread, #Hayden already mentioned in his comment.
Java will choose the most specific of the two methods provided (See the Java Language Specification)
If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.
As the class hierarchy for Frame is
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
An the class hierarchy for Window is
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
Frame is the most specific method, and then, your public static void hello(Frame f) will be chosen.

Can't call anonymous class method [duplicate]

This question already has answers here:
Using an arbitrarily defined method of an anonymous interface
(4 answers)
Closed 9 years ago.
I can imagine some very creative code in Java:
Object thing = new Object() {
public void speak() {
System.out.println("Hi!");
}
};
thing.speak();
Or even, to get the full closure effect, define a Function interface ... you get the idea?
Why doesn't this code work?
i believe you can do it like this :-
new Object() {
public void speak() {
System.out.println("Hi!");
}
}.speak();
may help you .
Not sure about the usefulness in this example, but some type of overriding method(s) on the original declaration is useful and because of it is overriding, you can call the methods. Otherwise in your case, just use the reflection as:
thing.getClass().getMethod("speak").invoke(thing);
and for the overriding method:
Object thing = new Object() {
public void toString() {
System.out.println("Hi! Me inside your mind!");
return "not today!";
}
};
thing.toString();

Categories

Resources