Cannot resolve a variable of same class but inside a static method [duplicate]












-1















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;
}

}









share|improve this question













marked as duplicate by Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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.




















    -1















    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;
    }

    }









    share|improve this question













    marked as duplicate by Sotirios Delimanolis java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    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.


















      -1












      -1








      -1








      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;
      }

      }









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 23:47









      Sayed Mustafa Fazeli

      92




      92




      marked as duplicate by Sotirios Delimanolis java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      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 java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      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.


























          2 Answers
          2






          active

          oldest

          votes


















          1















          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;
          }





          share|improve this answer



















          • 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



















          0














          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.






          share|improve this answer





















          • 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










          • That line only appears in the get_ipAddr() method in OP's code
            – GBlodgett
            Nov 21 at 0:05


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          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;
          }





          share|improve this answer



















          • 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















          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;
          }





          share|improve this answer



















          • 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








          1







          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;
          }





          share|improve this answer















          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;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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














          • 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













          0














          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.






          share|improve this answer





















          • 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










          • That line only appears in the get_ipAddr() method in OP's code
            – GBlodgett
            Nov 21 at 0:05
















          0














          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.






          share|improve this answer





















          • 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










          • That line only appears in the get_ipAddr() method in OP's code
            – GBlodgett
            Nov 21 at 0:05














          0












          0








          0






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 0:01









          Andrey Ilyunin

          1,259220




          1,259220












          • 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










          • That line only appears in the get_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










          • @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
















          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



          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft