About 173,000 results
Open links in new tab
  1. What is an instance variable in Java? - Stack Overflow

    Jan 7, 2021 · An instance variable is a variable that is a member of an instance of a class (i.e., associated with something created with a new), whereas a class variable is a member of the class itself. Every instance of a class will have its own copy of an instance variable, whereas there is only one of each static (or class) variable, associated with the ...

  2. java - Creating an instance using the class name and calling ...

    Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor. Something like: Object object = createInstance("mypackage.MyClass","MyAttributeValue");

  3. Using reflection in Java to create a new instance with the reference ...

    Dec 5, 2012 · All the examples I look at for reflection show creating a new instance of an unknown implementation, and casting that implementation to it's interface. The issue with this is that now you can't call any new methods (only overrides) on the implementing class, as your object reference variable has the interface type. Here is what I have:

  4. Create instance of generic type in Java? - Stack Overflow

    Sep 17, 2008 · But you can always delegate creation to some other class that knows how to create an instance - it can be Class<E> or your custom code like this. interface Factory<E>{ E create(); } class IntegerFactory implements Factory<Integer>{ private static int i = 0; Integer create() { return i++; } }

  5. How to declare a constant in Java? - Stack Overflow

    Oct 9, 2012 · You can use an enum type in Java 5 and onwards for the purpose you have described. It is type safe. A is an instance variable. (If it has the static modifier, then it becomes a static variable.) Constants just means the value doesn't change. Instance variables are data members belonging to the object and not the class. Instance variable ...

  6. Java: How to have an ArrayList as instance variable of an object?

    I'm working on a class project to build a little Connect4 game in Java. My current thinking is to have a class of Columns that have as instance variable a few integers (index, max. length, isFull?) and one ArrayList to receive both the integers above and the plays of each players (e.g., 1's and 0's standing for X's and O's).

  7. java - How to create a map instance variable - Stack Overflow

    Mar 12, 2014 · I've been trying to create a class that will model a scenario I've come up with. It will involve a map with string keys and values. I need to create an instance variable used to reference the map object, and a constructor that creates the empty map and assigns it …

  8. oop - What exactly is an instance in Java? - Stack Overflow

    Feb 26, 2011 · Reference is, in the Java context, a variable* - it is something pointing to an object/instance. For example, String s = null; - s is a reference, that currently references no instance, but can reference an instance of the String class. *Jon Skeet made a note about the difference between a variable and a reference. See his comment.

  9. java - How do I set default values for instance variables ... - Stack ...

    Apr 20, 2017 · You have two choices for setting the default value of an instance variable (sometimes called an "instance field"): Using an initializer on the declaration. Using an assignment in a constructor. Say I have a class Example and an instance variable answer whose default should be 42. I can either:

  10. java - Enum as instance variables - Stack Overflow

    Aug 4, 2012 · Before Java 5 the way to implement enums was to create a class with private constructor and public final fields of the same class initialised to specific values. Since Java 5, enum construct is effectively a sugar that does the same, and takes care of things like null values are not allowed, enum values become public static fields etc.

Refresh