14 Replies Latest reply: Feb 9, 2009 4:57 PM by JoachimSauer RSS

    Trouble with static

    807588
      I am trying to get my client to send out userName and passWord where value is but they can not be used in a static form
         public static void loginSystem() {
         
      
         
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               String userName = null;
               String passWord = null;
                 try {
                  System.out.print("UserName:");  
                  userName = br.readLine();
                  if (startSender = true) {
                  System.out.println(userName);
                  //send that sucker
                  System.out.println("packet would have sent");
                  }
                  
                  System.out.print("PassWord:");                 
                  passWord = br.readLine();
                   if (startSender = true) {
                  System.out.println(passWord);
                  //send that sucker
                  System.out.println("packet would have sent"); // find out if it is possible to hide password characters
                  }
                     } catch (IOException ioe) {
                  System.out.println("IO error ");
               System.exit(1);
            }
      
      
           
            }
      
        public static void connectToServerAlpha() {
          
        
          
               try {
                  System.out.println( "Attempting connection\n" );
            Client = new Socket( InetAddress.getByName("113355.no-ip.biz"), 5555 );    // create Socket to make connection to server
       
            System.out.println( "Connected to: "+ Client.getInetAddress().getHostName() ); // connection info
            
            while(true){
                OutputStream os = Client.getOutputStream();
          DataOutputStream dos = new DataOutputStream(os);
          dos.writeDouble(value);
      
          InputStream is = Client.getInputStream();
          DataInputStream dis = new DataInputStream(is);
      
          System.out.println(value);
          Client.close();
        } 
              }       
               catch (IOException ioe) {
                  System.out.println("connection alpha error");
                  ioe.printStackTrace();
            }
      
      
            
            }
        • 1. Re: Trouble with static
          807588
          I am not quiet sure if I understand your question. But I did spot this:
           if (startSender = true)
          This will always be true. I guess what you actually want is this:
           boolean startSender = false; 
            if(true == startSender)
          • 2. Re: Trouble with static
            807588
            As pointed out in another thread, this would be better:
            if (startSender)
            • 3. Re: Trouble with static
              807588
              flounder wrote:
              As pointed out in another thread, this would be better:
              if (startSender)
              Right. Nice.
              • 4. Re: Trouble with static
                807588
                   public static void loginSystem() {
                   
                
                   
                            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                         String userName = null;
                         String passWord = null;
                           try {
                            System.out.print("UserName:");  
                            userName = br.readLine();    //username can not be used in a static conte
                            if (startSender = true) {
                            System.out.println(userName);
                            //send that sucker
                            System.out.println("packet would have sent");
                            }
                            
                            System.out.print("PassWord:");                 
                            passWord = br.readLine();     //password can not be used in a static context
                             if (startSender = true) {
                            System.out.println(passWord);
                            //send that sucker
                            System.out.println("packet would have sent"); // find out if it is possible to hide password characters
                            }
                               } catch (IOException ioe) {
                            System.out.println("IO error ");
                         System.exit(1);
                      }
                
                
                     
                      }
                
                  public static void connectToServerAlpha() {
                    
                  
                    
                         try {
                            System.out.println( "Attempting connection\n" );
                      Client = new Socket( InetAddress.getByName("113355.no-ip.biz"), 5555 );    // create Socket to make connection to server
                 
                      System.out.println( "Connected to: "+ Client.getInetAddress().getHostName() ); // connection info
                      
                      while(true){
                          OutputStream os = Client.getOutputStream();
                    DataOutputStream dos = new DataOutputStream(os);
                    dos.writeDouble(THIS SHOULD BE PASSWORD);
                    dos.writeDouble(THIS SHOULD BE USERNAME); //pretty much
                
                    InputStream is = Client.getInputStream();
                    DataInputStream dis = new DataInputStream(is);
                
                    System.out.println(userName);
                    System.out.println(passWord);
                
                    Client.disconnect();
                  } 
                        }       
                         catch (IOException ioe) {
                            System.out.println("connection alpha error");
                            ioe.printStackTrace();
                      }
                I want to basically have
                public static string userName = null;
                public static string passWord = null;
                but I can't do this with buffered reader
                • 5. Re: Trouble with static
                  807588
                  Local variables cannot be public, cannot be static...because they are gone when the method they are in finishes.

                  I am a beginner so I could be wrong...
                  • 6. Re: Trouble with static
                    807588
                    String userName = null;
                    String passWord = null;

                    public static void loginSystem() {  
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    try {
                    System.out.print("UserName:");
                    userName = br.readLine(); //username can not be used in a static conte
                    if (startSender = true) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                    • 7. Re: Trouble with static
                      807588
                      sorry, that should be:

                      static String userName = null;
                      static String passWord = null;

                      public static void loginSystem() {
                      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                      try {
                      System.out.print("UserName:");
                      userName = br.readLine(); //username can not be used in a static conte
                      if (startSender = true) {


                      btw, having everything static is not the best idea
                      • 8. Re: Trouble with static
                        807588
                        Why have you chosen to make your methods static?
                        • 9. Re: Trouble with static
                          807588
                          because I call for them in other files and I'm a newb :D
                          • 10. Re: Trouble with static
                            807588
                            That is not a valid reason.
                            class Foo {
                                public int fooStuff() {
                                    return 42;
                                }
                            }
                            
                            class Bar {
                                public void barStuff() {
                                    Foo f = new Foo();
                                    System.out.println(f.fooStuff());
                                }
                            }
                            The method fooStuff is non-static and is called from another class.
                            • 11. Re: Trouble with static
                              807588
                              I'm still new to this stuff Ill change it
                              • 12. Re: Trouble with static
                                807588
                                objects Thanks alot it worked
                                • 13. Re: Trouble with static
                                  807588
                                  My suggestion to newbies is to:

                                  1. Write only one static method: main
                                  2. Have no static fields apart from "constants":
                                  public static final double H = 6.6260689633E-34;
                                  This helps you avoid the fatal attraction to make everything static.
                                  • 14. Re: Trouble with static
                                    JoachimSauer
                                    DrLaszloJamf wrote:
                                    My suggestion to newbies is to:

                                    1. Write only one static method: main
                                    2. Have no static fields apart from "constants":
                                    public static final double H = 6.6260689633E-34;
                                    +1

                                    Might be hard at first, but gets you to real OO programming a lot faster.