get current user id in apex

UserInfo() can be used to get the personal information about the currently logged-in user.
to get the id just use this method of userinfo - getuserid() : 
  ID CurrentuserId = userinfo.getuserID();

In the following example, the output text in the pageblock will dispaly the current logged in user's ID

  visualforce page


Similarly we can also get user name, profile and most of the other field values using userinfo.

For example, to get current logged in users name just use this method:
 String CurrentUserName = Userinfo.getuserName();


  1. From where i can access the user id now ??

  2. Hi Ankit ,here you get the user id in vf page. just preview vf page.

    [select id,name from user where id='vfpageid(ouputid)'] .