|
-
Mar 25th, 2002, 12:18 PM
#1
Thread Starter
Addicted Member
static method
I am trying to learn java on my own and I need your help.
I have the following question?
package NAME
public class NAME
{
int X;
int Y;
public static NAME CHANGE(INT A)
{
X=A;
Y=A;
return?????????
}
}
I KNOW I HAVE TO RETURN SOMETHING BUT I DON'T KNOW WHAT
thanks for the time
-
Mar 25th, 2002, 01:56 PM
#2
Fanatic Member
you can return anything, if your changing the name maybe you can just return "1" for successful
also, you can just do
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Mar 25th, 2002, 02:40 PM
#3
Thread Starter
Addicted Member
Nab
But the method
public static Name change(int x)
is asking to return type Name which is the classname
so what do I return
1?
Thanks for your time
-
Mar 25th, 2002, 03:44 PM
#4
Fanatic Member
change it to
Code:
public static void change(int x)
and you dont have to return anything
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Mar 25th, 2002, 04:54 PM
#5
Thread Starter
Addicted Member
Nab.
the function needs to remain as is
public static name(the name of the class) change(int x)
what would you return, because I tried with 1 and it didnt work
Thanks again
-
Mar 25th, 2002, 09:38 PM
#6
Fanatic Member
Maybe
what happens when you dont return anything?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Mar 25th, 2002, 09:49 PM
#7
If the function needs to remain how it is(although I can't really tell what it is doing), you can just return null. But you will get a NullPointerException if you try to use the object returned from the function.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 25th, 2002, 11:17 PM
#8
Dazed Member
I don't know im pretty lit right now but it looks like he is naming the method the same name as the class and wouldn't that make the compiler consider to be a constructor? It looks like a constructor and if it is it should be in the form of public/private
name(pramlist) throws{exceptions}
Last edited by Dilenger4; Mar 25th, 2002 at 11:21 PM.
-
Mar 25th, 2002, 11:19 PM
#9
Code:
public static NAME CHANGE(INT A)
I think the return type is NAME, and the function name is CHANGE.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 25th, 2002, 11:25 PM
#10
Dazed Member
Couldnt he return just return this ?
-
Mar 25th, 2002, 11:28 PM
#11
Originally posted by Dilenger4
Couldnt he return just return this ?
* shrug *
Like I said, I don't really follow what the function is doing.
But come to think of it, static methods don't have access to this, do they?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 25th, 2002, 11:34 PM
#12
Dazed Member
Code:
public class returntest{
public static void main(String[] args){
test t = test.returnme();
System.out.println(t.teststring);
}
}
class test{
String teststring = "Hello";
public static test returnme(){return new test();}
}
Last edited by Dilenger4; Mar 25th, 2002 at 11:47 PM.
-
Mar 25th, 2002, 11:36 PM
#13
Originally posted by Dilenger4
Code:
public class returntest{
public static void main(String[] args){
test t = new test();
System.out.println(t.teststring);
}
}
class test{
String teststring = "Hello";
public test returnme(){return this;}
}
Yeah, but returnme is not a static method,
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 25th, 2002, 11:38 PM
#14
Dazed Member
That code should work.
Last edited by Dilenger4; Mar 25th, 2002 at 11:48 PM.
-
Mar 25th, 2002, 11:55 PM
#15
Dazed Member
GUARO what is the need to have a static method that returns a refrence of it's enclosing class? Would't you want to go with using a constructor or are you being forced to use other specs?
-
Mar 26th, 2002, 12:14 AM
#16
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Mar 26th, 2002, 12:27 AM
#17
Dazed Member
Sorry i changed the code. Yes you are right though about a this refrence not being accessible from a static context.
-
Mar 26th, 2002, 04:57 AM
#18
Addicted Member
wouldn't you do...
Code:
package NAME
public class NAME
{
int X;
int Y;
public static NAME CHANGE(INT A)
{
NAME myName = new NAME();
myName.X=A;
myName.Y=A;
return myName;
}
}
I dont know why you would want to do that but that would fulfill your requirement.
It creates a new instance of a NAME and assigns the two values X & Y the value of A.
Oh and just in case you didn't know there are java naming conventions like Classes are all lower case apart from the first letter of every word. An article on Coding conventions can be found here... http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html
Sorry if you knew that and were just putting them as capitals for emphasis.
Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!
-
Mar 26th, 2002, 08:22 AM
#19
Thread Starter
Addicted Member
lady K
Thanks it worked. I understand now.
Thanks to all for your time, I really appreciated
-
Mar 26th, 2002, 06:50 PM
#20
Member
What is the point of having a static method which returns an instance of that classes, surely the following would be better?
Code:
public class Name
{
int X;
int Y;
public void change(int A)
{
X = A;
Y = A;
}
public static void main(String [] args)
{
Name myName = new Name();
myName.change(3);
}
}
then the contents of the main method could go anywhere....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|