Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Objects in java

Reply
Thread Tools

Objects in java

 
 
prasoonthegreat@gmail.com
Guest
Posts: n/a
 
      06-09-2009
I am a beginner to java.....

My question is ......Can objects in java only be created
dynamically????

Suppose i create an object of the String class

String x="PRASOON";

In this case is the object x of the String class is dynamically
created.....?????

And the other thing .......
Can x in the above case considered as reference to String object or an
object itself........?????
 
Reply With Quote
 
 
 
 
nagkumar nagkumar is offline
Junior Member
Join Date: Jun 2009
Location: Bangalore
Posts: 5
 
      06-09-2009
>My question is ......Can objects in java only be created
> dynamically????

Yes, all the object creation are done using the key word 'new'

>String x="PRASOON";
>In this case is the object x of the String class is dynamically
>created.....?????

Yes for the first time, but compiler does a optimisation of creating only one object with value "PRASOON" so that any further initialisation of references with same string value to use this only reference.


>Can x in the above case considered as reference to String object or an
object itself........?????

x is is only a reference to string object and not a value it self.

Regards,
Raja Nagendra Kumar,
C.T.O
tejasoft.com
-Java Product Development, Refactoring and Unit Testing Specialists
 
Reply With Quote
 
 
 
 
prasoonthegreat@gmail.com
Guest
Posts: n/a
 
      06-09-2009
On Jun 9, 4:10*pm, Thomas Pornin <(E-Mail Removed)> wrote:
> According to *<(E-Mail Removed)>:
>
> > Suppose i create an object of the String class

>
> > String x="PRASOON";

>
> > In this case is the object x of the String class is dynamically
> > created.....?????

>
> The following responses all apply:
> 1. Yes.
> 2. No.
> 3. Possibly.
> 4. The question makes no sense.
>
> In more details: Java source code expresses instructions which
> manipulates objects. The source code is processed by the Java compiler
> which turns it into a bunch of class files. The class files are then
> given to the Java Virtual Machine, which sets things up so that the
> instructions can be executed and find things "initialized". In the code
> above, when the program starts up, the String instance (of contents
> "PRASOON") already exists; it was created by the JVM when it loaded the
> class files.
>
> > Can x in the above case considered as reference to String object or an
> > object itself........?????

>
> In Java, values are references: a value (such as the one which is held
> in the "x" variable above) points to an instance, but is not the instance
> itself. Thus:
>
> * *String y = x;
>
> ... stores in "y" (another variable) a reference to the same instance.
>
> * * * * --Thomas Pornin


Wat about the first question???

Can objects in java only be created dynamically ....can't we create
objects at compile time?????
 
Reply With Quote
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      06-09-2009
On Jun 9, 9:38*am, (E-Mail Removed) wrote:
>
> Wat about the first question???
>
> Can objects in java only be created dynamically ....can't we create
> objects at compile time?????


No. Your program can only create objects when it runs, not when it is
compiled.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-09-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jun 9, 9:38 am, (E-Mail Removed) wrote:
>> Wat about the first question???
>>
>> Can objects in java only be created dynamically ....can't we create
>> objects at compile time?????

>
> No. Your program can only create objects when it runs, not when it is
> compiled.


Sort of. Constants, namely static final variables pointing to primitives or
Strings, are created at compile time in the class files. They are loaded into
the JVM at class initialization time. This can cause trouble when a constant
is redefined and only the defining class is recompiled. Classes that refer to
the constant also need recompilation when it changes. Otherwise the constant
doesn't change, it remains ... constant.

--
Lew
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      06-09-2009
(E-Mail Removed) wrote:

> Wat about the first question???
>
> Can objects in java only be created dynamically ....can't we create
> objects at compile time?????



While Thomas has a point, I think lord zoltar is correct, for the
broadest definitions of "dynamic" and "correct."

Java objects are always created dynamically, even if they are held in a
static reference. For example:

public class Main {
private final static String VERSION = "1.0";

//...
}

contains one static variable "VERSION" which is still created
dynamically at runtime. The string itself is referred to as a "literal"
not static.

Note that primitives usually follow the same pattern:

private final static int COUNT = 20;

I'd call COUNT a static variable, and 20 a literal. Primitives do
approach static creation in their implementation however.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-09-2009
On Jun 9, 12:13*pm, Mark Space <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Wat about the first question???

>
> > Can objects in java only be created dynamically ....can't we create
> > objects at compile time?????

>
> While Thomas has a point, I think lord zoltar is correct, for the
> broadest definitions of "dynamic" and "correct."
>
> Java objects are always created dynamically, even if they are held in a
> static reference. *For example:
>
> public class Main {
> * *private final static String VERSION = "1.0";
>
> * *//...
>
> }
>
> contains one static variable "VERSION" which is still created
> dynamically at runtime. *The string itself is referred to as a "literal"
> not static.
>


Since 'VERSION' is a constant, as defined by the JLS, that constant
gets compiled into the class bytecode and is not created as a variable
at runtime, but as a literal.

> Note that primitives usually follow the same pattern:
>
> * *private final static int COUNT = 20;
>
> I'd call COUNT a static variable, and 20 a literal. *Primitives do
> approach static creation in their implementation however.


Likewise. The variable 'COUNT' will not actually exist in the
bytecode but be replaced by its literal.

This is why a class that uses a public static final primitive or
String from another class has to be recompiled after the other class
is recompiled with a new value for the constant.

--
Lew

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      06-10-2009
Lew wrote:
> (E-Mail Removed) wrote:
>> On Jun 9, 9:38 am, (E-Mail Removed) wrote:
>>> Wat about the first question???
>>>
>>> Can objects in java only be created dynamically ....can't we create
>>> objects at compile time?????

>>
>> No. Your program can only create objects when it runs, not when it is
>> compiled.

>
> Sort of. Constants, namely static final variables pointing to
> primitives or Strings, are created at compile time in the class files.


No. They are not really objects when in the class file.

> They are loaded into the JVM at class initialization time.


Yes. Then they become objects.

Arne

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-10-2009
Lew wrote:
>> Sort of. Constants, namely static final variables pointing to
>> primitives or Strings, are created at compile time in the class files.


Arne Vajhøj wrote:
> No. They are not really objects when in the class file.


That's the "sort of" part.

>> They are loaded into the JVM at class initialization time.

>
> Yes. Then they become objects.


--
Lew
 
Reply With Quote
 
Prasoon
Guest
Posts: n/a
 
      07-04-2009
Suppose when we write

int x=5;

The memory is allocated for 'x' at compile time by the compiler or at
the run time by the jvm????
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Finding an accompanying java book to Objects first with java SIDSARAN@gmail.com Java 8 01-22-2007 07:14 AM
trouble handling java objects from inside java methods via C badduck Java 3 09-01-2005 05:20 AM
C++ objects that act like Java/C# objects. Steven T. Hatton C++ 8 06-28-2005 05:26 PM
JRuby: How does one keep Java objects as Java objects so they can be used in method calls? Steve Drach Ruby 3 06-19-2004 11:25 PM



Advertisments