Cannot resolve a variable of same class but inside a static method [duplicate]
This question already has an answer here:
What does a “Cannot find symbol” compilation error mean?
10 answers
My Problem: "return ipAddr"
ipAddr cannot be resolved to a variable.
So, is this because the method is static??
package oop.address;
import java.net.InetAddress;
public class address {
public static address createIP(String ip) {
try {
InetAddress ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
public InetAddress get_ipAddr(){
return ipAddr;
}
}
java class static
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 0:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What does a “Cannot find symbol” compilation error mean?
10 answers
My Problem: "return ipAddr"
ipAddr cannot be resolved to a variable.
So, is this because the method is static??
package oop.address;
import java.net.InetAddress;
public class address {
public static address createIP(String ip) {
try {
InetAddress ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
public InetAddress get_ipAddr(){
return ipAddr;
}
}
java class static
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 0:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
What does a “Cannot find symbol” compilation error mean?
10 answers
My Problem: "return ipAddr"
ipAddr cannot be resolved to a variable.
So, is this because the method is static??
package oop.address;
import java.net.InetAddress;
public class address {
public static address createIP(String ip) {
try {
InetAddress ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
public InetAddress get_ipAddr(){
return ipAddr;
}
}
java class static
This question already has an answer here:
What does a “Cannot find symbol” compilation error mean?
10 answers
My Problem: "return ipAddr"
ipAddr cannot be resolved to a variable.
So, is this because the method is static??
package oop.address;
import java.net.InetAddress;
public class address {
public static address createIP(String ip) {
try {
InetAddress ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
public InetAddress get_ipAddr(){
return ipAddr;
}
}
This question already has an answer here:
What does a “Cannot find symbol” compilation error mean?
10 answers
java class static
java class static
asked Nov 20 at 23:47
Sayed Mustafa Fazeli
92
92
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 0:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Sotirios Delimanolis
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 0:01
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
So, is this because the method is static??
No the fact that the method is static has nothing to do with it. The problem is that ipAddr is created in createIP(), and thus only exists in the scope of the method. I believe you wanted to make it a class variable:
private static InetAddress ipAddr;
public static address createIP(String ip) {
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
add a comment |
Every {} pair will create a custom scope with it's own "lifetime", which in the case of nested blocks in a method body will be always shorter than the outer scope.
Easy fix is:
public static address createIP(String ip) {
InetAddress ipAddr = null;
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return ipAddr;
}
But if you want to eliminate null, you have to either throw exception to the next stack frame (or you can just not to catch it) or specify a default value in the case of exception.
I believe the OP was also talking about theirget_ipAddr()method
– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in theget_ipAddr()method in OP's code
– GBlodgett
Nov 21 at 0:05
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
So, is this because the method is static??
No the fact that the method is static has nothing to do with it. The problem is that ipAddr is created in createIP(), and thus only exists in the scope of the method. I believe you wanted to make it a class variable:
private static InetAddress ipAddr;
public static address createIP(String ip) {
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
add a comment |
So, is this because the method is static??
No the fact that the method is static has nothing to do with it. The problem is that ipAddr is created in createIP(), and thus only exists in the scope of the method. I believe you wanted to make it a class variable:
private static InetAddress ipAddr;
public static address createIP(String ip) {
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
add a comment |
So, is this because the method is static??
No the fact that the method is static has nothing to do with it. The problem is that ipAddr is created in createIP(), and thus only exists in the scope of the method. I believe you wanted to make it a class variable:
private static InetAddress ipAddr;
public static address createIP(String ip) {
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
So, is this because the method is static??
No the fact that the method is static has nothing to do with it. The problem is that ipAddr is created in createIP(), and thus only exists in the scope of the method. I believe you wanted to make it a class variable:
private static InetAddress ipAddr;
public static address createIP(String ip) {
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return null;
}
edited Nov 20 at 23:54
answered Nov 20 at 23:51
GBlodgett
9,01341632
9,01341632
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
add a comment |
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
1
1
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
won't work: ipAddr is an instance variable whereas the method is static. non-static members can't be accessed from static methods.
– Andrey Ilyunin
Nov 20 at 23:52
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
@AndreyIlyunin You are correct. I edited my answer
– GBlodgett
Nov 20 at 23:55
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
why not just moving variable declaration out of the try scope?:)
– Andrey Ilyunin
Nov 20 at 23:56
add a comment |
Every {} pair will create a custom scope with it's own "lifetime", which in the case of nested blocks in a method body will be always shorter than the outer scope.
Easy fix is:
public static address createIP(String ip) {
InetAddress ipAddr = null;
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return ipAddr;
}
But if you want to eliminate null, you have to either throw exception to the next stack frame (or you can just not to catch it) or specify a default value in the case of exception.
I believe the OP was also talking about theirget_ipAddr()method
– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in theget_ipAddr()method in OP's code
– GBlodgett
Nov 21 at 0:05
add a comment |
Every {} pair will create a custom scope with it's own "lifetime", which in the case of nested blocks in a method body will be always shorter than the outer scope.
Easy fix is:
public static address createIP(String ip) {
InetAddress ipAddr = null;
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return ipAddr;
}
But if you want to eliminate null, you have to either throw exception to the next stack frame (or you can just not to catch it) or specify a default value in the case of exception.
I believe the OP was also talking about theirget_ipAddr()method
– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in theget_ipAddr()method in OP's code
– GBlodgett
Nov 21 at 0:05
add a comment |
Every {} pair will create a custom scope with it's own "lifetime", which in the case of nested blocks in a method body will be always shorter than the outer scope.
Easy fix is:
public static address createIP(String ip) {
InetAddress ipAddr = null;
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return ipAddr;
}
But if you want to eliminate null, you have to either throw exception to the next stack frame (or you can just not to catch it) or specify a default value in the case of exception.
Every {} pair will create a custom scope with it's own "lifetime", which in the case of nested blocks in a method body will be always shorter than the outer scope.
Easy fix is:
public static address createIP(String ip) {
InetAddress ipAddr = null;
try {
ipAddr = InetAddress.getByName(ip);
} catch (Exception e) {
System.out.println("Fehler");
}
return ipAddr;
}
But if you want to eliminate null, you have to either throw exception to the next stack frame (or you can just not to catch it) or specify a default value in the case of exception.
answered Nov 21 at 0:01
Andrey Ilyunin
1,259220
1,259220
I believe the OP was also talking about theirget_ipAddr()method
– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in theget_ipAddr()method in OP's code
– GBlodgett
Nov 21 at 0:05
add a comment |
I believe the OP was also talking about theirget_ipAddr()method
– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in theget_ipAddr()method in OP's code
– GBlodgett
Nov 21 at 0:05
I believe the OP was also talking about their
get_ipAddr() method– GBlodgett
Nov 21 at 0:03
I believe the OP was also talking about their
get_ipAddr() method– GBlodgett
Nov 21 at 0:03
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
@GBlodgett "My Problem: "return ipAddr" ipAddr cannot be resolved to a variable"
– Andrey Ilyunin
Nov 21 at 0:04
That line only appears in the
get_ipAddr() method in OP's code– GBlodgett
Nov 21 at 0:05
That line only appears in the
get_ipAddr() method in OP's code– GBlodgett
Nov 21 at 0:05
add a comment |