consider this simple servlet sample:
protected void doGet(HttpServletRequest request, HttpServletResponse response){
Cookie cookie = request.getCookie();
// do weird stuff with cookie object
}
I always wonder.. if you ... |
I need to programmatically find out exactly how much memory a given Java Object is occupying including the memory occupied by the objects that it is referencing.
I can generate a memory ... |
As a programmer I think of these as looking like "the java.lang.Object at address 1a234552" or similar for something like the s in
Object s = "hello";
Is this correct? Therefore are ... |
What happens if you save a reference to the current object during the finalize call? For example:
class foo {
...
public void finalize() {
...
|
Is pointer is just used for implementing java reference variable or how it is really implemented?
Below are the lines from Java language specification
4.3.1 Objects An object is a class ... |
If I instantiate an object in a main class, say:
SomeObject aRef = new SomeObject();
Then I instantiate another object from the main class, say:
AnotherObject xRef = new AnotherObject();
How can the instance of ... |
I have made two JAVA classes classA and classB in the same package. The classA consists of two labelFields and a button. And then I made 5 objects of classA inside ... |
|
class A is abstract and class B extends class A
now class A reference can hold object of class B,that is
A aObj = new B();
and assume class B has some extra methods....
like
class ...
|
We've created a caching layer to our J2EE-application. In this instance we use Ehcache. This has created a few challenges.
Let's take this example.
OrderItem orderitem = cache.getOrderItemByID("id");
OrderItem old_orderitem = cache.getOrderItemID("id");
orderitem.setStatus(1);
old_orderitem.setStatus(2);
If we're ... |
I got a question: Is it possible to get a list of currently instantiated objects from the VM?
I am using a framework and try to implement an event handler (Hibernate, ... |
What is the use of creating base class object using child class reference in Java
|
I've modified an object dumping method to avoid circual references causing a StackOverflow error. This is what I ended up with:
//returns all fields of the given object in a string
public ...
|
When person 1 become partner with person 3, person 2 should no longer have person 1 as partner and person 4 should no longer have person 3 as partner. How should ... |
In Java, I have an object that creates a button. Inside that button's onclicklistener I want to reference the object that created the button.
Is there some easy way I can do ... |
I am modifying a graph implementation to compute the all pairs shortest path matrix using Floyd's algorithm. The graph has both adjacency linked list and matrix implementations. For now ... |
Let's say I've got a type called Superstar. Now I want to have a method that does some work and edits some properties of a Superstar object.
Here are two ways of ... |
public class App1
{
public static void main(String[] args)
{
Point point_1 = new Point(5,5);
Point point_2 = new Point(7,8);
Circle circle_1 = new ...
|
why this is will not work, can any one give the exact answer for this one....
public class Manager
{
public static void main(String args[])
...
|
A reference to an Object on a 32 bit JVM (at least on Hotspot) takes up 4 bytes.
Does the 64 bit Hotspot JVM need 8 bytes? Or is some clever compression ... |
Is it possible to obtain all references to an object in Java.
What I need to check is if an object has removed all subscriptions for callbacks.
Thanks
|
When doing some sample codings with Java I came crosss ClassCast Exception, from where I cast the object to StaticClass. Can any explaing the what has happened here.
public void test5() throws ...
|
public void testFinally(){
System.out.println(setOne().toString());
}
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
return builder.append("Return");
}finally{
builder=null; /* ;) */
}
}
why output is CoolReturn, not null?
Regards,
Mahendra Athneria
|
The following code is part of a larger application:
public static void METHOD_NAME(Object setName, int setLength){
tryLoop:
for( ; ; ){
...
|
In Java, if I declare,
MyClass obj;
Is obj called a "reference" or an "object". I am not instantiating class here.
|
If you had something like the following:
class MyClass {
private class InnerClass {
}
}
In java. Does the inner class of the overhead of a ... |
What is the best practice for referencing nested objects?
Say I have the following:
class Outer {
private InnerA innerA;
//getters and setters
}
class InnerA {
private InnerB innerB;
//getters and setters
}
class ...
|
I'd like to know if there is a way to check how many references a Java object has. As far as I could check the only way to do that is ... |
During the execution of a Java application, are object references used by the runtime or are they stripped away at compile time?
I guess I could decompile class files and see how ... |
Scenario:
I have code that mutates objects and other code that doesn't, i.e. it treats the objects it works with as immutable.
But in a setting with parallel working tasks this works reliably ... |
A good analogy is a house. That house has rooms, and all of those rooms have people and objects. To begin with I only need to know the house exists with ... |
class Forest {
void lion(){
System.out.println("king of the jungle");
}
...
|
I have an object which I add to a List using the method theList.add(theObj).
If I now make changes to this object theObj, will these changes always be reflected in the object ... |
In the following code
public class Test {
public static void main(String[] args){
int [] arr = new int[]{1,2};
...
|
Consider the following two classes (one is a Main with main() method):
The VO class:
public class TheVO {
private String[] theValues = null;
/**
...
|
I have to execute a given String as JavaScript code, e.g. eval('Foo.setMessage("Hello!")'), from within a class called Engine. Here, setMessage() is a public static method of the Foo class. Because I ... |
The following does work because of the (Liskov) substitution principle, which says that if a reference is expected of an instance of a certain class then you may substitute a reference ... |
Let's say I have an instantiated object:
private static ArrayList<Boolean> P1SOLUTION = new ArrayList<Boolean>();
There will be similar objects such as P2SOLUTION, P3SOLUTION, etc.
I want the functionality of:
Arrays.toString(P1SOLUTION);
(Which prints the array as a ... |
I forgot some java concept.
PaymentData payment = basket.getPaymentData();
PaymentData newPayment = payment;
basket.unMaskCreditCardNumbers(payment);
basket.maskCreditCardNumbers(payment);
Here issue is when unmask the payment object, newPayment object also unmasking. if I mask payment object ... |
I have come across a self reference in some code I was looking at.
Example
TestObject selfReference = this;
Is there ever a good case in which you would need a self reference in ... |
Will the car_object_1 be able to garbage collected? Somebody maintain that as car_object_1 has two reference so it will never be garaged collected. Is it true?
Car createACar()
{
Car c = ...
|
I need to maintain a list of object references so that every time the object is initialized its object reference is stored.
The design I'm considering for this is to create ... |
String myString = "this";
//string is immutable
myString.concat(" that");
//a new object is created but not assigned to anything
System.out.println(myString); //prints out "this"
I would prefer a compile time error - why is this not the ... |
I am unable to understand how this works
public void addToRule(Rule r) {
if (!getRuleList().contains(r)) {
getRuleList().addElement(r);
}
}
If I ... |
I am designing a game engine in Java.
At the core of this engine exist the two classes Asset and Attribute, where an Asset has a list of Attributes. Most Attributes need ... |
I am currently creating a big project, and as such I would like to have everything worked out very well and as efficient as possible.
In my project, I have a class ... |
I have a JSON string looking like that (simplified):
[
{ "id":1, "friends":[2] },
{ "id":2, "friends":[1,3] },
{ "id":3, "friends":[] }
]
The content of friends are ids ... |
I want to search a class file for all Methods and its references for specific Object types or Keyword.
For Example:
Say I have class file called myclassfile.java which holds several methods as ... |
After reading the books, surfing the nets regarding the type of references in Java, I still have some doubts (or I may have interpreted the concept wrong).
It would be a great ... |
On the one hand;
String first = "thing";
String second = "thing";
if(first == second)
System.out.print( "Same things" ); //this is printed
On the other hand;
String first = "thing";
String second = new String("thing");
if(first ...
|
How can I swap the contents of two Integer wrappers?
void swap(Integer a,Integer b){
/*We can't use this as it will not reflect in calling program,and i know why
...
|
I've got a simple question. In this code below, why is s3's value still printed although I set it to null before. It seems gargbage collector won't get called.
public class Test ...
|
|
Hi, I am using for some time MICO CORBA, for a project, and I am satisfied. Now I am planning to switch from BOA to POA, to make the project portable across ORBs, but I fail to get the details of the differences between object reference (BOA) and Object IDs (POA). I know what an object reference does (what is used ... |
Hi, i have searched the web, tried for days to get this to work, but it doesn't. So i really hope someone can help a newbie. 1. A client on a remote machine creates a Person object. 2. The Server needs a reference or something to this object so that it can call methods on it. How can i implement this? ... |
class X{ private String y; public void setY(String y){ this.y=y; } public String getY(){ return this.y; } } class ABC{ public static void main(String args[]){ X dummy=new X(); dummy.setString("Hello"); ABC.method1(dummy.getString()); } public static void method1(String abcd){ // I want to get a reference to dummy object here. //Is this possible.If so how ? } } |
I would like to know if its possible to get a reference to an object without using an array. For example, lets say this code declares a 3 Integaer3: Integer a = new Integer(1); Integer b = new Integer(2); Integer c = new Integer(3); Instead of using: System.out.println(a); System.out.println(b); System.out.println(c); I want to output the same using the Strings "a", "b", ... |
I would like to know if its possible to get a reference to an object without using an array. For example, lets say this code declares a 3 Integaer3: Integer a = new Integer(1); Integer b = new Integer(2); Integer c = new Integer(3); Instead of using: System.out.println(a); System.out.println(b); System.out.println(c); I want to output the same using the Strings "a", "b", ... |
No, they are exactly the same. Local variables live on the stack, and memory on the stack gets reserved when the method is entered. As a consequence, a local variable declaration doesn't cost any time. References also don't get garbage collected, ever - only objects get. The number of objects that get created and need to be collected is the same ... |
Hi , I have one doubt in the following code. class A { } class B extends A. 1. A objA=new A(); 2. A obj=new B(); 3. B objB=new A(); The first two line will work without giving any error. but for line 3, it will say compile time error. May i know the reason why this is giving an error? ... |
Dear All, I was refering to a Java certification book ( A PROGRAMMER'S GUIDE TO JAVE CERTIFICATION , BY KHALID A. MUGHAL AND ROLF W.RASMUSSEN) and the Topic is Object Oriented Programming ( Chapter 6 ) where it has specified " A CAST ONLY CHANGES THE TYPE OF REFERENCE NOT THE CLASS OF THE OBJECT ". Can some one help me ... |
//-------------------------- class1 object1 = new class1(); class1 object2; object2=object1; //------------------------------ now in the code given above, object1 and object2 are acting as references to the same object. so whatever value that you change in object1 will be reflected in object2 also. now how to prevent this? or how to create object2 as an exact copy of object1 but an autonomous object? ... |
We have Java 5 on our 64 bit machine and the memory consumption of OR mapping objects is significantly (40%-50%) larger on 64 bit machine as compared to 32 bitmachine. I was wondering if anybody knows if JVM implementaion for 64 bit machine uses long instead of int for object references? Or any other reason? Any inputs, insight would be great!! ... |
hi all, i have one problem.. first see this code class A { int i=10; public static void main(String[] args) { System.out.println("Hello World!"); } } class B extends A { int i=20; public static void main(String[] args) { A o1=new B(); System.out.println("Hello World!"+o1.i); } } when i am pritning value of i it is giving me 10 can any one help ... |
Hi, I was glancing over Sierra/Bates Java 2 cert book and came across the following code example explaining pass-by-value semantics: void bar() { Foo f = new Foo(); doStuff(f); } void doStuff(Foo g) { g = new Foo(); } From this code: 1. are g and f both referring to the SAME object? or are they 2 separate instances? 2. if ... |
|
|
I did use a profiler....actually they're logging statements that calculate the time for each method invocation. I see that some methods take time....but then there's no algorithm that needs improvement. Its more like the requirements now want the program to run faster because of a change in user input. And I'm trying to solve that without hindering correctness. [ April 13, ... |
They do mysteriously map to object addresses on the heap. Objects when serialised can be moved off the heap. They live in files or attached to ByteArrayOutputStreams or flying around the network. Whenever you deserialise these objects you have to reassign a variable name to them, otherwise they will disappear into some cyber-limbo until the garbage collector finds them. And when ... |
Hello, I can't seem to understand object references. For example, if I have a superclass A and a subclass B. What does this line of code denote. A a = new A(); A a = new B(); Like at (1), why is it not possible to call sup.hello? Thank you very much for your time public class PrivateTest { public static ... |
class Point1 { int x, y; // two instance variables for point's coordinates Point1(int x, int y) {this.x=x; this.y=y;} // } class ColoredPoint1 { Point1 classpoint; //Composition; instance variable char color; //new instance variable ColoredPoint1(Point1 classpoint, char c) // { this.classpoint = classpoint; color = c; } public void equalizetoPointWithRef(ColoredPoint1 targetpoint) { classpoint = targetpoint; //equalize by change of ... |
Not exactly what u expect but i finally understand overloading methods , static members & object references . Might aswell post it for u lot to see . Put the following codes in their individual files & well compile 'em .Run 'em n see what happens !!! U can run 'em individually aswell . class XTC{//this is code one static String ... |
i know this doesn't makes much sense, but i try to relate the prob using an example...i try my best to explain... Lets say i have 2 classes : A Dog class and a Cat class A Dog class which consists of a Leg object (There is a Leg class which have 'number of legs' as an attribute as well as ... |
I'll try to explain by way of example and diagram: Object obj = new Object(); With the above statement, two things are created: 1) a variable that can refer to an Object (obj) 2) a new instance of the Object class. Stack: Heap: | | | obj -------------> (Object) |_____| Your code gains access to the object via the reference 'obj'. ... |
Suppose you have a class called Car. When you instantiate an object of type Car, the code looks something like this: Car c; c = new Car(); or equivalently, Car c = new car(); I understands that c is a reference variable that points to the object that you just created. But if c is simply a pointer to the actual ... |
Hello, I have typed the following question 3 times in an effort to get it making sense. What I want to know is this: Is it possible for an instance of objB to find out what instance of an objA is referencing it? In particular, I want to be able to call a method from the referencing objA from objB. (Gee, ... |
Would someone be willing to tell me if I have forgotten anything in this example code? I'm trying to comment on everything so that I get this right from the get-go. class Fruit { //the name of the class is "Fruit" //variables can be either primitive type or class type //class type variables are called "reference variables" //primitive type variables are ... |
When we create an object with "new Thing()" the JVM finds the class description of Thing, allocates some memory for its variables and initializes any variables that need it. (The JVM does much more we can ignore right now.) So I guess you could say the "object instance" is that chunk of initialized memory. As you do things to the object ... |
Hi, I come from a C++ world and now moving to JAVA. Consider piece of Code. void foo(String str) { str = "Hello" + str; } When one calls foo, the string passed remains unchanged. Similar thing in C++ (passing object my reference) modifies the object. Can you explain to me in detail what is happening internally here? I understand that ... |
I had a couple quick questions about how to create references to objects. 1. In the first scenario, I want to create a reference to an object of the type NewObject. I want to set this reference equal to the return value of the createNewObject method of the newObjectFactory object. Would my code look like this?: NewObject sampleNewObject = (NewObject) newObjectFactory.createNewObject(); ... |
An object (or instance of a class) is the actual object in memory. A reference is a pointer to that object. References are stored in variables while objects just sit in memory somewhere. Compare this with integers. An integer variable holds the actual integer value completely; it doesn't point to the value. To clear your down, note that "new Act()" returns ... |
|
I have some code that will pass in a couple of parameters and call a method. This method will get parent/child objects. The web page is a list of reports and I want to return the report object for each one when the report title ends with "Revenue per Day". How do I reference the CurrentObject that is returned from the ... |
I'm still working my way through Bruce Eckels Thinking in Java and have come up with a query about reference objects. Bruce says: You use reference objects when you want to continue to hold onto a reference to that object, but you also want to allow the garbage collector to release that object. Thus, you have a way to go on ... |
I will be the first to admit that I am really bad at java and I struggle heavily with it. Our teacher gave us this diagram of how to make our simulation(first my teacher is pretty much useless and impossible to get ahold of). It has a lot of different classes and all of them reference each other. For example my ... |
Well, thanks for thr reply. My application is a complex one in JADE where I am developing a multi agent system. So, I can't really create a new class that binds everyhting togther (although its the ideal way to do it). But how do I solve my problem? Class RecruitmentOfficer creates an object of JobsDB. JobsDB has a hashmap.(the only hashmap ... |
hi, I have a doubt and want to clear it. Please help. I have created an object "sks" of class named "skillsearch" in the same class. I have created another object "conn" of class "myConn" which is an innner class of class "skillSearch". When I refer to "conn" object, I use it as skillsearch.sks.conn But, when I set the reference to ... |
I have very basic Java Question about object assignment. interface +++++++++++++++++++++++++++++++++++++++++++++ public interface Animatable { public void animate(); } class +++++++++++++++++++++++++++++++++++++++++++++ public class GameShape { public void displayShape() { System.out.println("Displaying Shape"); } } class which extends GameShape and implements interface Animatable ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public class PlayerPiece extends GameShape implements Animatable{ public void movePiece() { System.out.println("moving game piece"); } public void animate() { ... |
A reference is an implicit pointer to an area of the heap where a certain object exists. How and why would an object know about its reference(s). What would happen if an object had 10 references to it? The values of cb1 and cb2 is a value that represents an area of memory(ie what it is pointing to). In Java there ... |
Originally posted by Keith Lynn: objs is just an object reference. If you assign it to point to a new object, it will not affect the reference in the list. I could have sworn that at one time, if I did: Object obj1 = new Object(); Object obj2 = obj1; obj1 = null; then, obj2 would be null. But this is ... |
Java has compile-time type checking. That is, you have to declare what class of object each reference points to, and you can only (directly[*]) call methods applicable to that class. This is different to some other languages that only have run-time type checking. In Python or JavaScript, for instance, you do not declare what type a variable has. Each method call ... |
Hi Remko, Local variable must be initialized before using. the statement what you said is correct.... by looking at the code Integer intvar; while(...){ intvar=it.next(); //Here Error comes,Var->Not initialized } dont consider intvar as a local object."See where it is declared".it is an instance reference variable(for the object goi to create) then it will automatically get initialized to null.. may be ... |
|
Originally posted by Abhinav Srivastava: One object and one reference. The same 'honeyPot' reference is used in both arrays. It's just one, even though it appears 4 times in each array. Actually, the reference is copied. There is still just one object, but the number of references increases. You can tell by assigning null to one of the array members; one ... |
I'm plodding through the "Head First Java - 2nd Ed." book, and just did the "Popular Objects" exercise (p. 267, ans. at bottom of p.270) at the end of Chapter 9. The exercise directs the reader to examine some code that creates some objects and a great number of references to them, and to determine which single object ends up with ... |
|
class AA { public void aMethod() { System.out.println("In AA"); } } class BB extends AA { public void aMethod() { System.out.println("In BB"); } public void aMethodOR() { System.out.println("In BB Over Rideded"); } } class CC { public void aMethod() { System.out.println("In CC"); } public static void main(String sa[]) { AA a = new AA(); a.aMethod();// 1 BB b = new BB(); ... |
Hi members, We know that whenever we try to print the reference variable of any class , the toString() method from current class or it's parent class is executed. class MyClass{ public String toString(){ System.out.println("Executing the toString()"); return ("returned from toString()"); } public static void main(String[] args){ MyClass ob = new MyClass(); System.out.println(ob); } } It happens to the reference variable ... |
when you say MyObject foo = new MyObject(); two things are created. an object is made on the heap somewhere. you don't have direct access to this. this is the house. a reference called "foo" is made. this is the notecard, which you DO have. the '=' then writes the address on the card. so in this case, there is an ... |
For your own particular class "A", the object won't do anything, and you can't use it, and it will get thrown away. But for some other different classes, this doesn't have to be the case. For example, imagine if the constructor did something a little more interesting? For example, class A extends Thread { public A() { start(); } public void ... |
Hey guys and gals, I just finished my first course in Java programming (I'm a Biomedical Engineering major, but wanted to play with code a bit more). I have a big question that has been taunting me throughout the class that I've wanted to get off my chest. I'm going to set up a hypothetical situation to make my question easier ... |