Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java code

Status
Not open for further replies.

plork123

Programmer
Mar 8, 2004
121
GB


Hello

Can anyone help me out here

I have a class, with one method

public class getUniqueId
{
public String uniqueId()
{
UID uniqueUserId = new UID();
//System.out.println("User id :"+uniqueUserId);

return uniqueUserId.toString();
}
}

I have another class file where i want to call the uniqueId() method

This doen't work

String uid = getUniqueId.uniqueId();

I'm being a bit think here, can someone help me out

Cheers

 
To access a method of a class, you need to instantiate an object.

Code:
getUniqueId agetUniqueId = new getUniqueId();
String uid = agetUniqueId.uniqueId();

The othre way is to declare the uniqueId() method static.

Cheers,

Dian



 
because static methods are created regardless of whether or not there is an instance of the object. same goes for static variables.

you have static methods and instance methods.. what you have in your code is an instance method. if there is no instance, there is no instance method..
 
each instance of an object gets its own copy of an instance method.. therefore, if there is no instance, there is no method!

sorry.. forgot to metnion that.. can't seem to edit my post
 
On a separate point, the naming of your class and the method are better t'other way around (for good OO semantics). So
Code:
public class UniqueId
{
    public String getUniqueId()
    {      
        UID uniqueUserId = new UID();
        //System.out.println("User id :"+uniqueUserId); 
        
        return uniqueUserId.toString();
    }
}

Methods are 'actions' and are best named a such, such as 'get...', 'set..', 'draw...', 'put...', 'kick...' and so on.

Classes are 'things' and should really be named as nouns.


Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top