Click to See Complete Forum and Search --> : static method
GUARO
Mar 25th, 2002, 12:18 PM
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
nabeels786
Mar 25th, 2002, 01:56 PM
you can return anything, if your changing the name maybe you can just return "1" for successful
also, you can just do
X=Y=A;
GUARO
Mar 25th, 2002, 02:40 PM
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
nabeels786
Mar 25th, 2002, 03:44 PM
change it to
public static void change(int x)
and you dont have to return anything
GUARO
Mar 25th, 2002, 04:54 PM
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
nabeels786
Mar 25th, 2002, 09:38 PM
Maybe
return (NAME)1;
what happens when you dont return anything?
crptcblade
Mar 25th, 2002, 09:49 PM
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.
:)
Dillinger4
Mar 25th, 2002, 11:17 PM
I don't know im pretty lit right now :D 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}
crptcblade
Mar 25th, 2002, 11:19 PM
public static NAME CHANGE(INT A)
I think the return type is NAME, and the function name is CHANGE.
;)
Dillinger4
Mar 25th, 2002, 11:25 PM
Couldnt he return just return this ?
crptcblade
Mar 25th, 2002, 11:28 PM
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?
Dillinger4
Mar 25th, 2002, 11:34 PM
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();}
}
crptcblade
Mar 25th, 2002, 11:36 PM
Originally posted by Dilenger4
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, ;)
Dillinger4
Mar 25th, 2002, 11:38 PM
That code should work. :)
Dillinger4
Mar 25th, 2002, 11:55 PM
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?
public Change() {}
crptcblade
Mar 26th, 2002, 12:14 AM
Originally posted by Dilenger4
That code should work. :)
I don't doubt that it will ;). But if you change it to a static method, it should error during compilation.
(Look at me using the big words)
:)
Dillinger4
Mar 26th, 2002, 12:27 AM
Sorry i changed the code. Yes you are right though about a this refrence not being accessible from a static context.
Mrs Kensington
Mar 26th, 2002, 04:57 AM
wouldn't you do...
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/html/CodeConvTOC.doc.html
Sorry if you knew that and were just putting them as capitals for emphasis.
GUARO
Mar 26th, 2002, 08:22 AM
lady K
Thanks it worked. I understand now.
Thanks to all for your time, I really appreciated
daveyboy
Mar 26th, 2002, 06:50 PM
What is the point of having a static method which returns an instance of that classes, surely the following would be better?
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....
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.