• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • 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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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