What Does Instance Mean
Jan 16, 2015
in a set of instructions they keep referring to instance versions of things I've heard of before ie "private instance array of String references" (wtf is a string reference?) or "instance string variable" so what does all this mean?
View Replies
ADVERTISEMENT
Mar 7, 2014
I've Parent and child(extends Parent) class To initialize the constructors, I'm injecting from google.juice#injector. Let me show the code,
Parent.class
public class Parent{
private Animal animal;
@inject
Parent(Animal animal){
this.animal = animal;
[code].....
When I do this, ClassCastException is happening. Why is it so? Is there any way to convert instance of parent to child instance.
View Replies
View Related
Sep 18, 2014
.I was reading head first java book and saw a barbell question on page no. 280,question-"what if you want to write a class in such a way that only one instance of it can be created,and anyone who wants to use an instance of the class will always use that one,single instance?"
View Replies
View Related
Apr 7, 2013
In the following example, I compute new_amount_d in 2 different ways and I get 2 different values although it should be the same in theory.
Is there a way to get the same value with BigDecimal for instance?
public class num004
{
public static void main(String[] args) {
double amount_d = 1000;
double interest_rate_d = 0.03;
[Code] ....
View Replies
View Related
Feb 23, 2015
I am working on a project and it asks me to "Provide appropriate names and data types for each of the instance variables. Maintain two GVdie objects" under class fields. I am unsure as to what is being asked when asking for two objects as instance variables and how I would write that...
View Replies
View Related
Feb 23, 2015
I have a method that accepts JSONArray as parameter and returns the values of it as ArrayList Object. My question which of these ways is appropriate in populating the ArrayList object this method populates the arraylist upon creation of object (I don't know what the right term to use, but as netbeans IDE suggest, JSONArray object should be final since it was used in inner class.).
private List<String> getStringList(final JSONArray jsonArr) {
return new ArrayList<String>() {
{
try {
for (int i = 0; i < jsonArr.length(); i++) {
add(jsonArr.getString(i));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
};
}
this second method is the usual way of populating collection
private List<String> getStringList(JSONArray jsonArr) {
List<String> strList = new ArrayList<String>();
try {
for (int i = 0; i < jsonArr.length(); i++) {
strList.add(jsonArr.getString(i));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
What are the advantages and disadvantages between the two? like which is faster? or which consumed larger memory?
View Replies
View Related
Jul 13, 2014
I have been working on a program that is meant to use a class' instructions in a program to add a value to a variable, save it, and present it. This is my class
public class Car
{
//FIELDS
private int yearModel;
private String make;
private int speed;
//METHODS
public Car(int carYearModel, String carMake)
[Code] .....
Whenever I call the accelerate method, a value of 5 is to be added to the speed variable. But whenever I call accelerate, it doesn't increase! I just don't understand why not. I've tried different renditions of adding 5 to speed and it doesn't quite work. I don't get any errors when I compile, just runtime, when it doesn't add 5 to speed.
View Replies
View Related
Apr 7, 2014
So far in my assignment I have successfully opened a text file. However I am required to do more:
1) As each line of text (containing names and ages) is read a new Runner object is created with its instance variables set thus: ! (Runner class already created )!
- name : set directly set from the value in the file
- agaGroup : can be worked out from the given ages:
< 18 should be 'junior'
> 55 should be 'senior'
the rest should be 'standard'
2) the instance of Runner should be added to the list referenced by the instance variable runners.
I have used if statements to create the junior list, however I do not see the full list of names and ages in the variable runners as I am requested to.
I am sure there is a for loop involved somewhere but I do not know how to:
a) use the for loop in my method
add a new Runner object with the variable mentioned.
I include the code I have done so far as a file - p.s I use Bluej.
public class MarathonAdmin
{
// instance variables
private String runners;
private String age;
[Code] ....
View Replies
View Related
Sep 25, 2013
Can I declare a class as
public static final?
Because I can declare a variable as public static final pi=3.14;
View Replies
View Related
Sep 26, 2014
I want to create a new instance of a Java model class for each new request coming to a servlet.
How to do that without doing that in doGet() or doPost().
View Replies
View Related
Oct 24, 2014
I have the following 2 classes:
class Address {
private int a;
public void set_a(int a) {
this.a = a;
}
}
class Person {
private Address address;
}
How do i access the method set_a() (through the "address" in Person) from another class which contains main() ?
View Replies
View Related
Feb 5, 2015
Let's pretend I'm working on an RPG. Like in all RPGs, there are items found all throughout the imaginary world. Each player and NPC can obtain an item. My question will concern those items.
In other words, I'd like to use instances of a class in multiple places of the code. Each instance will have its own, individual values of instance variables, with one obvious exception: itemQuantity should have a different value in playerInventory, npcInventory, etc. Also, I'd like a list of all items that can be found in the game. This list doesn't need itemQuantity at all.
class Items {
String itemName;
float itemWeight;
int itemQuantity;
[Code] ....
The question is: should I really make itemQuantity an instance variable of the Item class? It seems as though for each copy of the Item class I should create a separate copy with different value of itemQuantity, but that's not very efficient. Where is the error in my logic?
What's important is that there may be plenty items in a game and a player may be given power to create new items during the course of the game.
View Replies
View Related
May 5, 2014
I'm just wondering why variables in interface can't be instance scope?
interface Test{
int a;
}
And then
Test test = new TestImpl();
test.a=13;
Yes, it violates OO, but I don't see why this is not possible? Since interface is not an implementation, therefore it can;t have any instance scope variable. I can't find the correlation of interface being abstract and being able to hold instance scope variable. There's gotta be another reason. I'm just curious about any programmatic limitation, not deliberate design constraint. the example of programmatic limitation is like when Java forbids multiple inheritance since when different parents have the exact same method, then the child will have trouble determining which method to run at runtime.
View Replies
View Related
Jan 25, 2014
I have 1 textfield and 1 button on a JFrame and having 10 such frames stored in ArrayList al and getting the JFrame instance from traversing the ArrayList at execution time ,So is there a way to access textfield using JFrame instance or i have to name the textfield diffrently 10 times for each frame .
View Replies
View Related
May 6, 2015
Alright, I have a JavaFX gui that is creating a new instance of data calculation to graph in a chart; however, the data is not updating each time the Platform.runLater() feature executes. Each time an event occurs, a new instance with the same variable name occurs. I use to get methods to retrieve the data I want, so shouldn't the values update each time the new instance is created? This is a very condensed version of what happens with the event, but this is what is not working correctly.
Event:
solarPlot = new SolarTracker();
solarPlot.getElevation();
solarPlot.getAzimuth();
Class constructor :
public SolarTracker() {
[Code] .....
View Replies
View Related
Jul 26, 2014
I am working with a JFormattedTextField. After adding the text of the FormattedTextField to an LinkedList i want to read it out and sum it up. So I have a problem to convert the String to and integer...
Example:
23.00 - to 23.00
+ 11.00 - to 11.00
--> 34.00
I have tried it with splitting the string but it didn't work. How to do it?
View Replies
View Related
May 23, 2014
Is there anything wrong on my code?
import java.util.*;
public class Card
{
private String description;
private String suit;
private int value;
private static final String [] cardName {"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten",
"Jace","Queen","King","Joker"};
[Code]...
View Replies
View Related
Sep 16, 2014
Is there anyway to iterate an enum type without an instance. As some context, consider the following code:
Java Code: public interface GenericChecker
{
public bool isValid(String str);
} mh_sh_highlight_all('java'); Java Code: public class EnumChecker<T extends Enum<T> > extends GenericChecker
{
private Class<T> enumType; //No instance
[code]....
toString method of the enum types has been overridden so that it returns the name assigned to the enum rather than the enum name itself. For example an enum might be SOME_ENUM("Assigned name"), therefore toString returns "Assigned name" rather than "SOME_ENUM". The idea is that a field from a table can be handed to the isValid(String) function on the GenericChecker base, and the derived class will then check to see if the field matches valid data as far as it is concerned.Thus, I can create a whole bunch of checkers easliy:
Java Code: GenericChecker checker1 = EnumChecker<EnumType1>();
GenericChecker checker2 = EnumChecker<EnumType2>();
GenericChecker checker3 = EnumChecker<EnumType3>();
GenericChecker checker4 = SomeOtherChecker(); mh_sh_highlight_all('java');
The problem is, if I use the EnumChecker then the expression "enumType.getEnumConstants()" in the isValid function blows up because enumType is null.
View Replies
View Related
Oct 9, 2014
What is the correct way of describing the type of instance variables?
It is obvious in the case of a primitive type
Example: private int number;
The (data)type of number is integer.
What if the instance variable is an object or a wrapper?
example: private Person person;
the (data?)type of person is an object of the type/class Person?
example: private Integer number;
View Replies
View Related
Mar 22, 2014
Java Code:
package Threads;
// THIS PROGRAM WILL HAVE TWO THREADS i.e. "main" AND ANOTHER THREAD (SYSTEM WILL NAME IT "Thread-0"
//THE STORY IS THAT WE WILL START Thread-0 FROM main AND LET IT EXECUTE.
//main WILL WAIT AND LET IT EXECUTE FOR 5 MINUTES.
//IF IT FINISHES ITS EXECUTION BEFORE 5 MINUTES, WELL AND GOOD;
//BUT IF IT DOESN'T, WE WILL INTERRUPT IT.
//AFTER INTERRUPTION, WE WILL DECIDE TO WAIT INDEFINITELY.
public class SimpleThreadsCopy {
public static void threadMessage(String s){
String sThreadName= Thread.currentThread().getName();
System.out.format("%s: %s%n", sThreadName, s);
[Code] ....
The statement against which I have written many *'s gives the following error.
No enclosing instance of type SimpleThreadsCopy is accessible. Must qualify the allocation with an enclosing instance of type SimpleThreadsCopy (e.g. x.new A() where x is an instance of SimpleThreadsCopy).
Now that a similar "error-free" code is given here, what's wrong with this piece of code and what should I do about it?
Trying to understand the error statement, I replaced the erroneous statement with
Java Code : Thread t= new Thread(new SimpleThreadsCopy().new MessageLoop()); mh_sh_highlight_all('java');
And the error got fixed. From that I understand that the inner class is just kinda a nonstatic member of the outer class and it will be accessed by the objects of the outer class only.
But then why doesn't the code in the tutorial give an error?
View Replies
View Related
Jan 8, 2014
Do inherited methods use their instance variables or do they use the ones in the method that inherits them?
For example, Class B extends Class A. Class A and B both have the instance variable "potato". A client program tries to use method "cut" using an object of Class B, but class B has no cut method. So, class B uses the "cut" method inherited from class A. What I want to know is will that cut class A's potato or class B's?
View Replies
View Related
May 27, 2014
There is method in the class of my application and there are four other class which is running in different machine means different jvm and these classes are accessing the method of my class. then how to protect my method from accessing other class at the same time. I am not able to use synchronized keyword because this works only for single jvm.
View Replies
View Related
Mar 25, 2014
I'm having trouble to fully understand the difference between instance and class variables.
I though it was all about the static, i.e:
int age; // instance variable
static int age; // class variable
What's behind this?
View Replies
View Related
Jul 10, 2014
Implementation that proves that instance variables are not thread safe ....
View Replies
View Related
Oct 26, 2014
I can't figure out where to create the StringHandler object. My code should take a string as input, then create StringHandler object ord with the string input. This should repeat until cancel is pressed, then ord should be sent to the Utskrift-method (a print method).
If I do like this, null is also sent to Utskrift. I dont want that to happen.
If I put StringHandler last in the loop ord can not be resolved.
String text = "";
while (text != null){
text = showInputDialog(null, "Enter text:");
StringHandler ord = new StringHandler(text);
if (text == null){
[Utskrift(ord.getNumber(), ord.getString(), ord.getWords());
break;
}
}
View Replies
View Related
Feb 23, 2015
This isn't asking to make a Singleton, where I want only one type of a type of window period.
It's not where I want only one instance of the program itself open at a time. It's more along the lines of, though I don't care if there is more than one Microsoft Word open at the same time, I don't want more than one instance of blablabla.docx to be open at once.
In other words, I'd need to scan my system or my list of open windows of a particular type to make sure that no two ones where equals is true will match up.
I was going to do it to associate it with a particular name with a window (it's sort of an addressbook, though not quite.) I'd use a JFrame subclass to make these windows that have a name go with them. For now, unless I find the great need for this project that there might be someone with the exact same name around here, I'm going to use equals() with name on this. I can type cast and whatnot.
However, how do I get to know what windows are open at once so I can check all my list of open windows, and I'd prefer to check only ones of certain types, though I may have to check them all indirectly perhaps, before I check the names.
I'm wondering is it a getChildren() of the main window or something else? How do I know what all of my open windows are?
I'd prefer to have to check as few as possible and not have to check unnecessary ones.
As for the structure of my thing that would click that would open the new window, I'm not sure yet. I'm almost leaning JLabel, though it's a bit less sophisticated, as it might be more difficult to add to a JList (maybe I"m wrong on thinking on that. It's been a little while since I last worked with JLists.)
(I think I can set list data, but the problem is, even using an ArrayList, it only takes an array for this as a param and ArrayList returns an Array of type Object, which means a lot of pesky type casting every time just to add or remove a name.)
It does have Vector, but I thought Vector was being phased out.
Anyway, without having too much code given, as I'm not sure how I'll set this up yet, how do you get access to all of the windows being opened to be able to check them for certain traits?
View Replies
View Related