• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Simple java code

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
0
plork123
Asked:
plork123
  • 3
  • 2
  • 2
  • +1
1 Solution
 
aozarovCommented:
String uid = new getUniqueId().uniqueId();
0
 
aozarovCommented:
Though I would recommend you in this case to make this method static.
public String uniqueId() -> public static String uniqueId()
And then this can remain the same
String uid = getUniqueId.uniqueId();

0
 
sudhakar_koundinyaCommented:
or Your class should be changed like this


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

and call it as  
String uid = getUniqueId.uniqueId();
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudhakar_koundinyaCommented:
oh, you have already mentioned  that :-)
0
 
plork123Author Commented:


Thanks for the replies

What's the benefit of making it static?

0
 
mightyoneCommented:
normally you would do:

getUniqueId a = new getUniqueId();//btw due to java namingconvention this should be GetUniqueId
String idstring = a.uniqueId();

By making it static it can be called in a static way like

String idstring = getUniqueId.uniqueid();

making it static also means that this methods will only exist once in the JVM so if you have several Threads calling this method it might not l0onger be unique.... (Singletons and synchronizing can help...)
0
 
aozarovCommented:
>> making it static also means that this methods will only exist once in the JVM
Normally yes, but just for correctness (and I hope I am not confusing plork123), it is actually once per classLoader.
0
 
mightyoneCommented:
... psst don't tell him too much or he will never ever write java code again....



;-)
0
 
plork123Author Commented:



Thanks very much.  I think !!!!!!!

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now