Work this out with a pencil and paper: What is the Mouse object in the cat before and after each bit of code? Then you can see whether there is a significant difference or not. And by "w" did you mean "m"? Some people like lots of invocations joined together by dots, some don't. It depends on what the methods return; ... |
|
|
|
I'm placing a large number of Star objects in a hashset container. I want to be able to reference and remove a particular Star from that set at some later date, but I have no references for individual stars in the code below. How does one reference a particular element of the hashset without explicitly naming it during instantiation? public class ... |
First of all, please use the code tags, it's easier to read your code Second, methods can override each other, but fields get accessed by the object you created. In your code: A obj = new B(); the object obj is of type A, that means obj.i gets the field "i" from A. If you change your code to B obj ... |
|
|
|
|
i saw a class with the following structure: class Line { Color color; Point start; Point end; ... ... } class Point { int x, y; Line l; // which this point is belonged to void aMethod() { ... ... System.out.println(l.color); } } in the real application, they are more complicated. but they do have the structure like above: Line has ... |
|
Now lets say for the sake of argument that Animal does define some non-final, non-private, overridable method called beSmallAndObnoxiouslyCute() and your Pomeranian subclass overrides that Animal beSmallAndObnoxiouslyCute() implementation with its own. Now you can do what I showed in the code snippet up above but it may surprise you to see (or then again it might not) that at runtime, the ... |
|
Hi all!! I have a question about getting the current reference name, i know i can use getClass().getName() to get the name of the current class. but how do i get the actuall name of that particular instance? Television tv1 = new Television(); System.out.println(getClass().getName());only gives me "Television" however i would like it to say "tv1" IS there a way to do ... |
The question is incomplete. You cannot say whether this will compile without knowing the class declarations of X and Y. If we use String and Integer instead, eclipse (at least) tells me I cannot cast a String to an Integer. If it were possible that the object referenced were compatible with the type being cast, then I think you could do ... |
|
Hey guys, Sorry for the indescriptive title, couldn't think of a better one. I'm currently learning from a book, "Learn Java 6 in 21 Days". I'm at the topic of object that reference to other objects instead of creating new ones. Take the following code: class EqualsTester { public static void main(String[] args) { String str1, str2; str1 = "Free the ... |
Hi, I did not understand what you meant by "re-usability of reference". The whole concept of polymorphism in java relies on java's ability to use interface/base class reference to reference to sub class objects. Also, programming to an interface rather than to the actual implementation is always seen as a best practice. This would allow the developer to replace one implementation ... |
hi , i have a question for all you java programming bosses , the question is when actually we can say that an object has lost all its reference and , now it is gonna collected by garbage collector please use a basic " sample " program to explain . for eg: class abc abc a = new abc(); so now ... |
Hi Recently, I read that every object reference (irrespective of the actual object size in heap) has a size of 32 bytes. byte offset type description 0 ptr object heap address 4 ptr int32 method table address array length 8 uint32 Size 12 uint32 Type 16 ptr next handle 20 ptr gray list 24 uint32 space marker 28 uint32 Lock Is ... |
Hello All, I am trying to understand object reference and garbage collection. Here is what confuses me. 1) what is difference between object and object reference? Box mybox = new Box(); Here I think that mybox is object of class Box. Now what is object reference? 2) garbage collection states "when there is no reference to object the memory occupied by ... |
...What I'm asking is, if you assign two variables to point to the same object, and then you change the object (instead of re-assigning the variable like I mistakenly did in my example), won't that change the object for both references? Here's a better example: .... Won't arr2 be sorted as well when this is through? |
|
I think your problem is a call by value problem. filex[0] is a reference to an element in the array. You are setting the value of _object in the method. When the method exits, _object goes away. What line is line 24? The println()? A better technique would be to change the method to return the object: filex[0] = createObject('s',4.99); // ... |
|
Hello Friends. Please consider the following code: Java Code: //Finding shortest possible path. int sizeOfStack = potentialPaths.get(0).size(); Stack> opWrongWay = new Stack>(); for(Stack> s : potentialPaths) { if(s.size() < sizeOfStack) { sizeOfStack = s.size(); [B]opWrongWay = s;[/B] } } Notice the line of code that I have made bold. There I was attempting to copy/clone the Stack [s] to a different ... |
I am trying to make a simple text rpg after studying java for a bit to try and solidify what I have learned so far. I was under the impression that if you make a parent(base class) reference and then calling a child(derived) class constructor that it would cast the parent to a child. To some extent I understand the process ... |
|
okay. Overall, once a customer has been added to the system, the display needs to be updated. The above method code is a part of the GUI; MainScreen.java but within the controller for this class;CustomerScreenHandler, it needs to update the display on the GUI. This may help a little more. Java Code: public void addNewCustomer(String name, String number, String address) { ... |
Hi, I just started programming in Java. So if I understand correctly, objects are always pass-by-reference. Now I am storing 3 integers in an object called Beepers. (int count, street, avenue;) So I got two Beepers objects stored in the array and I used get(index) to get a reference to two different objects. I thought I would get an integer back, ... |
|
|
As I said above, I do disagree with you on that one. I think it's very good practice to explicitly show that your code is using null as an acceptable and default value for an attribute. It reminds people (even yourself down the road) that a certain attribute should be anticipated to be null while in a valid state for the ... |
|
From what I understand, an object's reference is a pointer to the object's location within memory. Is it possible to obtain this reference value? If so, then can I assign this value to another object of the same type? I'm sure there are other ways of accomplishing this, but I'm really not interested in those other ways. I more interested in ... |
A variable1 = new A(); // Legal --1 B variable2 = new B(); // Legal --2 A variable3 = new B(); // Legal, since B is a subclass of A --3 B variable4 = new A(); // Illegal --4 I understood 1 and 2,but not 3 and 4.could you please explain how? read few books ...but still confused |
|
|
Static fields are used to contain information common to all objects of the class. Each object may have different values for it's fields, but every object of the class will see the same value for a static field. So, if you change the value of a static field, every object sees that change. If an object changes its regular fields, no ... |
I don't think "call" and "pass" are interchangeable, but "call by " and "pass by " seem to be. I've never heard the term "call by" to refer to how methods are invoked--that is "call by reference" meaning we invoke objects' methods through their references is a new one on me. |
|
Thanks for responding my enquiry... My attention was not too be sarcastic on the question I asked, or the piece of code I placed. However It's unforunte you felt I was being sarcastic. But why did you felt I was being sarcastic. I always feel it's better if keeps things simple, getting to the point. I know in emails or correspondence ... |
Java is pass-by-value. Recommended reading: [http://www.javaranch.com/campfire/StoryCups.jsp] [http://www.javaranch.com/campfire/StoryPassBy.jsp] This topic has been done to death too many times already. I'm moving this thread to New to Java and locking this it. Jasvinder.Singh, please confine your questions to the New to Java forum until such time as you can regard yourself as a Java Programmer. db |
I have to create a for loop, which loops through each of these tree objects and sets the colour to green after a dialog prompts the user confirm("Is treeObject1 green?"); if the user clicks yes, treeObject1.setGreen(true) is called. Is there any way I can use the incrementer in the for loop - i - in conjunction with these treeObject references? e.g ... |
Nah, Not really,I just have a pdf containing some SCJP questions and I am trying out some. Me Too have found some links : [JLS 3.0|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.2] [Reference Rules|http://www.xyzws.com/Javafaq/what-are-the-forward-reference-rules/9] There is a bug report posted in the above link which has something to do with the problem in hand : [Java Bug Report|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6425594] Awaiting experts reviews on these. Also,Duke Stars rewarded to ... |
1. This code is posted as a revert to your claim that "all" methods will be accessible to the subclass. 2. Writing code so that you don't get compiler and runtime error is not a great idea to write your code,no ? 3. If the explanations by jverd made sense to you, your query should be resolved. 4. Also even if ... |
Hey guys! I have a class Class1 that has a JLabel object and I want it to display the name of a BasketballPlayer object when he/she has the ball. There is another class Class2 that keeps track of which player has the ball with a reference to that player called currPlayer. Whenever that player has the ball currPlayer will be a ... |
|
Wow, that's a bit harsh. You know, having a short fuse is a bad thing in life - it positively won't get you anywhere. Think of the price you pay for having it. If ine is driving and someone else cuts you off, a short fuse might lead one to retaliate. This could lead to an accident, nowdays being shot at, ... |
Hi, I understand that using the == operator to compare two String object references in Java will yield false for two different object references even when the contents are the same, and that you are supposed to use the method .equals() instead. However, in my program the == operator actually works for two different object references that point to the same ... |
cotton.m wrote: Sorry I don't feel like playing with you today uj. I specifically did not say "yes" and nothing more in reply #5 because I knew you would complain about cloning. But then I thought there are other ways to get a copy as well. For example, Serialization. So I just left it as get a deep copy. But you ... |
Declaring a local variable inside or outside the loop makes no difference at execution time. All local variables are allocated in one shot at method entry. Putting a declaration inside or outside the loop doesn't matter--it's still a single variable. In terms of programming style, however, the general rule is to declare a variable in the narrowest scope possible. So if ... |
Right, so you just make sure the spawner doesn't retain a collection of threads after they are finished, the obvious way is to make sure the collection of spawned workers is only in a local variable, but removing threads from the collection as you call join on them would be fine. Basically, you don't need to worry about it. It's all ... |
P.S. I apologize if I'm being overly specific, but many years of experience with Internet forums tells me that if I weren't, I would be more likely to get the following responses than an actual response to the question: - comments/criticisms about the value/usefulness of doing it - suggestions that I do something different - questions about why I want to ... |
kquizak wrote: i have a vector ... and i wanna get the object directly (based on the vector i have a html table with the values and the reference string... As it has already been mentioned the text you showed is not a "reference string". It's simply the output of the default toString() method. The second value is the result of ... |
Yes, it makes sense and I agree to it. But I started it initially just because of thinking in the direction that == actually is capable of handling null reference to an object. e.g. Integer ii = null; if(null == ii){ // This code works fine. } //So, by a Java rule null is never equals to anything. //Hence, it is ... |
its part of a genetic algorithm im creating a child population and the two will be combined and best individuals will be selected for the next generation google ngsa2 no thankfully no vb jst feeling a bit blond this evening. this problem was preceded by an argument with the debugger. i was certain it was wrong! jst one of those days!! ... |
In your first example you changed the local copy of the reference, which didn't have any effect on the outside. In your second example you changed the object that is pointed to by the local copy of the reference. Since the local copy of the reference points to the same object as the original reference you see the effect outside the ... |
I need a way to find all the the objects that are holding a reference to a particular class instance. Basically I have some objects that are not getting GC'ed, and I can't figure out who is holding a reference to them. For example: I want to answer the question: For a given class Foo, show me every class (or instance) ... |
I have an class A that writes to a file. And how he writes to the file depends on how it is configured to behave. The configuration is stored in a text file. Depending on what package the object that is calling A belongs to, it will take different actions depending on the configuration. |
That works, but it's not using num which should contain the passed in reference to count in the main class. This is the problem, apparently Integer is immutable so using it to pass a reference to a variable containing a number in the main method is pointless because that reference cannot be used to then later change the Integer. So how ... |
|
Hi, This is a problem I came across today while developing a simple prototype. Imagine I have a class Boy which has a one-to-many relation with a class Dog. The class Boy has the following methods: getName(), setName(), getAge(), setAge(), getDogs(). The class Dog has the following methods: getName(), getOwner() When the application starts I load a Boy from the database ... |
Case 1, don't do it. Case 2, based on a member method, then probably don't do it. Case 3 depends on the source. If I am writing code that interfaces between layers then I am very likely to check it. It isn't a matter of what you are sure of now but rather as well what you can be sure of ... |
|
/** * TicketMachine models a naive ticket machine that issues * flat-fare tickets. * The price of a ticket is specified via the constructor. * It is a naive machine in the sense that it trusts its users * to insert enough money before trying to print a ticket. * It also assumes that users enter sensible amounts. * * @author ... |
|
Local variables live on the stack and their lifetime is the life of the execution of the method in which they are declared. They are not given any default value. Member variables live on the heap, "inside" the object that declares them. Their lifetime is that of the enclosing object. They are given a default value of 0/false/null when the object ... |
Lets say I have an application dealing with cards, and it uses Card beans. In my application I am going to instantiate 20,000 separate card instances. Is the total memory consumed by this 20,000 x 4 = 80,000 bytes (for the object references to the cards?). Now considering that Card has rank and suit (which are enums, so static), each card ... |
|
Hi, After creating a File object i am passing the same to other classes (these are actually event Listeners in the application) and these classes saves the File object reference as their instance variable. So if i change property of the File object anywhere in the application no doubt that change would be reflected in those File object references in Listener ... |
|
George, I have to disagree. Your post gives the impression that one has to do a fair amount of work to manage memory, when most of the time, you don't have to do anything special at all. Usually just letting variables go out of scope, or reassigning them as part of your normal code flow is sufficient. Sometimes you have to ... |
Did the virtual machine allocate ram if a class was referenced by many other classes for every reference of the class or only for the one created class. For example you got a class MakeSomeStuff with lots of lists, Arrays etc. This needs some heap space. If the class was referenced by 10 other classes, do they need heap memory too? ... |
I'm using JPOX and I'm having an error when I save an object that has a collection of elements and one of them has been deleted. It seems like I have still some references to that object, that's why the exception is thrown... so it will help me a lot if I can stop (or not) the execution of the application ... |
|
Hey, I want to traverse on the object references and on their references... I know the bfs/dfs algorithm for traverse on a graph. My problem is how to implement it in Java. How to know what are the references (i think reflection is too much complicated here) I need to implement it in order to have the ability to create diff ... |
Going through the JLS, they say nothing specific to object references. All the examples tend to use ints. Vector might be a bad object to use as well, since it's synchronized. Let's change it to ArrayList. I would say that a volatile ArrayList could have it's contents dirty, since only the arraylist reference is volatile, not the references to it's elements. ... |
The JVM has a "register" stack, (not to be confused with the method call stackframe.) This is called the "operand stack", and it's what bytecode operators act on, References to the Integer object are pushed onto the stack. Operand stack contents will be: 0: new #2; //class java/lang/Integer Integer 3: dup Integer, Integer 4: iconst_5 Integer, Integer, 5 5: invokespecial #3; ... |
|
|
808072 wrote: I would like to find a way to use the ArrayList to print out the products instead of adding a method to my Product class to return all product info as String. Why should the ArrayList know anything about how to display a Product? And why would you want to avoid adding that method to Product? If you want ... |
Think of your variables as people pointing at boxes. You create a box with 1 in it and have to point at it You create a box with 2 in it and have to2 point at it You then get to point at the same box as to2 You change the value in the box to2 points at 3 (remember to ... |