Results 1 to 3 of 3
- 11-26-2011, 01:33 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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:
and the testclass: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(); } } }
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).Java Code:public class RauteTest { public static void main(String args[]) { //int hoehe; //String zeichen; //zeichen = "*"; //hoehe = 7; Raute.Raute(7, "x"); } }
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 ;)
- 11-26-2011, 01:39 AM #2
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
-
Re: call a method from another class without static?
Create a Raute object and then call the non-static method off of the object.
Similar Threads
-
Inner class method call
By d915172 in forum New To JavaReplies: 3Last Post: 11-11-2010, 09:40 PM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
How to call a class within a method
By Manfizy in forum New To JavaReplies: 3Last Post: 03-19-2009, 12:34 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks