Results 1 to 8 of 8
  1. #1
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default transfer data from static to non-static method

    Good morning from Greece,

    I am new to Java and i have a problem.

    I try to transfer some data from class to class and from static to non-static method and i cant.
    I have the next 3 classes.
    and i need to transfer the data ("test" & i) from the First.java to Second.java
    But the problem is that i should not make an object of second.java class in the First.java, because i have some Jframe and i loose it after i create the object.
    And one more that should not change is that run me class must not change.
    The second problem is that the method SecondB cant change to static or nothing all, because it is continue to call other methods.

    The question is how i can tranfer my data from the static method SecondA to the non-static method SecondB ?????

    runMe.java
    Java Code:
    public class runMe {   
        public static void main(String args[]){  
        Second sc = newSecond(); 
        First fs = new First();
        }    
    }
    First.java
    Java Code:
    public class First { 
        public First(){    
            for (int i = 0; i < 10; i++) {
                Second.SecondA("test",i);
            }  
        }    
    }
    Second.java
    Java Code:
    public class Second extends JFrame{  
    public Temp ()
    	{   
    		setTitle( "my Frame" );
    		setSize( 310, 130 );
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
        public static void SecondA(String a, int i){
        SecondB(a,i); 
        }
        public void SecondB(String aa, int ii){
        System.out.println(aa+"->"+ii);
        }  
    }

    Thank you in Advanced

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: transfer data from static to non-static method

    example of how to call static methods vs instances

    Java Code:
    public class Test {
    	
    	
    
    	public static void main(String[] args){
    		
    		//call static methods in static class
    		OtherClass.staticMethod();
    		
    		//call instance of a class(non static method) from static class
    		OtherClass oc = new OtherClass();
    		oc.nonStaticMethod();
    		
    		
    	}
    	
    	
    
    	
    	
    }
    
    class OtherClass{
    	public static void staticMethod(){
    		
    	}
    	
    	public void nonStaticMethod(){
    		
    	}
    }

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: transfer data from static to non-static method

    If your interested in accessing a non static method within the same class.

    Java Code:
    public class Test {
    	
    	
    
    	public static void main(String[] args){
    		
    		//this would not work
    		//nonStatic();
    		
    		//this would work
    		Test selfCopy = new Test();
    		selfCopy.nonStatic();
    		
    		
    	}
    	
    	public void nonStatic(){
    		
    	}
    
    }
    basically static means it belongs to the class itself and non static means it belongs to a instance of the class. Thats why if your calling a static method you use ClassName.staticMethod(); and with non Static methods you call it with ClassInstanceName.nonStaticMethod(); but when calling a static method within the same class you don't need to say ClassName.staticMethod(); you can just say staticMethod(); because it knows that your already using that class.
    Last edited by killutch; 10-21-2012 at 01:06 AM.

  4. #4
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: transfer data from static to non-static method

    Like i told, the cant make an instance, because my class us JFrame extended end when i make second time instance i lost some components. this is the difficult problem. I have been try the instances.

    The problem is how to call a non-static method from static in the same class without instances.
    Or
    How to call non-static method from other non-static method but from different classes, and again without instances.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: transfer data from static to non-static method

    The solution is still to call Second's secondB method on a Second instance, but yes you should not create another Second instance to do this. Instead pass a reference to the actual viewed Second instance to where it is needed. For instance:

    Java Code:
    public class RunMe {   
       public static void main(String args[]) {  
          Second sc = new Second(); 
          First fs = new First(sc); // pass Second reference into First
       }    
    }
    Java Code:
    public class First { 
       public First(Second second) {    
          for (int i = 0; i < 10; i++) {
             // use Second reference into modified static method
             Second.secondA(second, "test",i);
          }  
       }    
    }
    Java Code:
    public class Second extends JFrame{  
       public Second () {   
          setTitle( "my Frame" );
          setSize( 310, 130 );
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    
       // note change to static method. It now requires a
       // Second parameter be passed in so it can use this instance
       public static void secondA(Second second, String a, int i) {
          second.secondB(a,i); 
       }
       public void secondB(String aa, int ii) {
          System.out.println(aa+"->"+ii);
       }  
    }
    Also, please learn and adhere to Java naming conventions. Class names begin with a capital letter and method names with lower case letters. This is important if you want others (namely *us*) to be able to more easily understand your code and help you.

  6. #6
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: transfer data from static to non-static method

    Thank you guys.

    It work for me.
    You are perfect.

    Thanks a lot.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: transfer data from static to non-static method

    You're welcome. Glad you've got things working.

  8. #8
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: transfer data from static to non-static method

    With experts like you, how could not to work.
    Thanks again

Similar Threads

  1. Replies: 3
    Last Post: 05-31-2012, 07:56 PM
  2. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  3. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  4. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •