• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use of new keyword..

 
deepesh mathur
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys i want to know that where the new keyword can be used (if possible kindly let me know all of its uses)
 
abhishek pendkay
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new keyword is used when you want to create an object of any class ie to call the constructor of that class for eg to create an object of the Thread class you would use the new keyword like this
Thread myThread = new Thread()
 
deepesh mathur
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey abhishek,
i think new keyword is also used for instating
1. arrays
2. string
(kindly tell any more if you know)
 
abhishek pendkay
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes new is also used for creating arrays and strings because in JAVA arrays and strings are objects.... everything in java (except for primitive data types) is an object and that is why to make an array or string you use the new keyword.... although JAVA does allow you to create strings without using the new keyword but even then strings in JAVA are objects
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
guys i want to know that where the new keyword can be used (if possible kindly let me know all of its uses)
--------------------------------------------------------------------------------
The 'new' keyword can be used in conjunction with an object reference, or without

eg. String s = new String("new String");
or
new String("another String");

Note that both ways of using the new keyword to instantiate an object are legal, but in the second case, you will not be able to access the String object later since there is no reference.
 
deepesh mathur
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey ,
abhishek and vidhya thanks a lot..
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that new may also be used with slightly different syntax to instantiate an inner class:

[ November 05, 2007: Message edited by: Kelvin Lim ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic