Results 1 to 3 of 3
  1. #1
    pancake718 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default call a method from another class without static?

    Hey there,

    i've tried to write a class and a testclass to.. test it.

    it looks like this:


    Java Code:
    public class Raute{
    
    	static int i, j, hoehe;
    	String zeichen;
    	
    public static void Raute(int hoehe, String zeichen) {
    
    for (i = 0; i < (hoehe/2)+1; i++) {
     
        for (j = 0 ; j < hoehe; j++) {
     
            if ( (j > (hoehe/2) - i) && (j < (hoehe/2) + i) ) {
                System.out.print(zeichen);
            }
            else {
                System.out.print(" ");
            }
     
    	}
     
    System.out.println();
     
    }
    
    for(i=(hoehe/2)+1; i>0; i--) {
    	
    	for(j=0; j<hoehe; j++) {
    		if( (j > (hoehe/2) - i) && (j < (hoehe/2) + i) ) {
    			System.out.print(zeichen);
    		} else {
    			System.out.print(" ");
    		}
    	}
    	System.out.println();
    }
    }	
    
    
    }
    and the testclass:

    Java Code:
    public class RauteTest {
    	
    	public static void main(String args[]) {
    
    		//int hoehe;
    		//String zeichen;
    		//zeichen = "*";
    		//hoehe = 7;
    	
    		Raute.Raute(7, "x");
    
    	}	
    }
    Now the problem is, if I don't write static in front of my first class, I cannot compile the test class (cannot find symbol-error).
    And if I have it static, then I have to write static in front of my int-variables aswell, which I don't really like, b/c I've read that if you do so, you really just use that for constants. And mine aren't constants.
    So I don't know how to call my method from the testclass without making it a static class!

    Is there a way to do so? Or is it alright like this, but then what does this static-stuff really mean? I've tried to read in in java api, but I didn't really understand what was going on.

    Can someone help? =)


    -- pancake



    P.S. sorry for any mistakes in grammar or spelling, I'm not lazy or anything.. I'm german ;)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: call a method from another class without static?

    You shouldn't have a method name the same as the class name. That makes for confusing code to read.
    If you want to call a non-static method in a class, you need first to create an instance of the class and use that instance to call the method.
    ClaNm aRef = new ClasNm(); // create an object and set a reference to it
    aRef.theMethod(); // call a method in that class

    There are several discussion of static at this site. Find static and read all about it.
    The Java Language Specification, Third Edition - TOC

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

Similar Threads

  1. Inner class method call
    By d915172 in forum New To Java
    Replies: 3
    Last Post: 11-11-2010, 09:40 PM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •