Describing The Type Of Instance Variables?

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


ADVERTISEMENT

New Object With Instance Variables

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

Accessing Instance Variables And Methods

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

Do Inherited Methods Use Their Instance Variables?

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

Object / Methods - Private Instance Variables

Jun 30, 2014

so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...

for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.

i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?

in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?

Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?

class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;

[code]...

View Replies View Related

Accessing Variables From Array Instance Of Class?

Jan 26, 2015

I am having some problem accessing variables from an array instance of a class. Heres what i have done;

In the main class:

Example obj[]= new Example[4];

In the main class constructor:

obj[0] = new Example(0);
obj[1] = new Example(1);
obj[2] = new Example(2);
obj[3] = new Example(3);

In the main update() method:

if(condition)
//update

In the Example class constructor:

private boolean change = false;

In the Example class update() method:

if(x >20)
change= true;

Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?

View Replies View Related

Why Overridden Doesn't Apply To Instance Variables

Mar 15, 2014

why overridden doesn't apply to variables. However, instance variables are stored inside the object.I ran below program and expected to print "two" but it gets printed "one".

class SupCont {
String s = "one";
}
class Cont extends SupCont {
public static void main(String a[]) {
String s = "two";
SupCont c = new Cont();
System.out.println(c.s);
}
}

View Replies View Related

Creating Instance Variables And Constructors For Map Class

Mar 27, 2014

I have to create an application that deals with maps.

I first have to create the instance variables for the class.

So very simply if my hashmap is going to consist of football clubs and players. Football clubs being a string value for the key and players being a set of strings for the values. How would I go about creating the instance variable in a class for this?

I can't seem to find anything that specifically deals with instance variables and constructors for maps.

View Replies View Related

Making A Rectangle - Manipulation Of Instance Variables

Sep 11, 2014

I am suppose to create a rectangle and I have created two classes; Rectangle.java and RectangleTester.Java.

So far my code for the class Rentangle.java is:

package edu.sbcc.hw2;
public class Rectangle {
private int width = 25;
private int height = 25;
public rectangle(int xcoord, int ycoord, int thewidth, int theheight) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;

So for my assignment I need two instance variables for height and width for which I have, but it says in the assignment I need methods (settings and getters /mutators and accessors that allow manipulation of my instance variables which is a little confusing. Do I put these methods on Rectangle.java or RectangleTester.java.

The same goes for the calculateArea, where am I suppose to put this?

View Replies View Related

Instance Variables Can Be Declared In Class Level Before Or After Use

Jun 3, 2014

From the tutorial:instance variables can be declared in class level before or after use.But the code below does not compile. Why?

Java Code:

public class MainApp {
i=5;
public static void main(String[] args) {
System.out.println("Hello World");
}
int i;
} mh_sh_highlight_all('java');

View Replies View Related

Instance Variables Passed In As Arguments To Static Methods?

Feb 19, 2011

I thought static methods could never use instance variables, because they wouldn't know which instance to look at.

From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."

Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?

View Replies View Related

Create A Class That Includes Three Pieces Of Information As Instance Variables

Oct 9, 2014

Creating a class. Specification are given as:

Create a class called Employee that includes three pieces of information as instance variables:

-Employee ID (string type)
-first name (string type) (default value 'John')
-last name (string type) (default value 'Smith') and
-monthly salary (type double).
-No argument constructor that initializes the three instance variables. The employee id should be generated using the following process:

The employee id should be a combination of first initial, last initial and a number starting from 10001 for the first employee and increasing by one for each employee. e.g. if John Smith is the first employee then its id will be JS10001 and if George Brown is the second employee then its id will be GB10002

-Provide get and set methods for each instance variable. The set method for monthly salary should ensure that its value remains positive - if an attempt is made to assign a negative value, leave the original value.

View Replies View Related

Iterate Enum Type Without Instance

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

Servlets :: Thread Safety Should Not Use Any Variables Or Objects At Instance / Class Level

Jun 3, 2014

As web server has multiple threads to serve client requests in Thread Pool & to ensure Thread Safety we should not use any variables or Objects at Instance/Class level.But in case of Session Variable which one is the Best Practice as the Session object is used by all the requests to have the same Session ID.

My Code :

public class MyServlet extends HttpServlet {
private static Logger log = Logger.getLogger(ClientRegistrationServlet.class);
private HttpSession session; /* This is used at Instance Level*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

[code]....

View Replies View Related

How To Make Sure Only One Particular Type Of Instance Is Open At A Time

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

How To Pass Object Type To A Method / Then Create Instance

Aug 9, 2014

Essentially, the code I want is:

public void randomCreate(ParentObject obj){
int x = random(0-4); //pseudo
int y = random(0-4); //pseudo
create new ParentObj(x,y);
}

ParentObject is actually abstract, so you would only ever pass one of its children objects to it, and a child object of that type would be created. It seems like there should be a way to pass a type, rather than an object, and then create an instance later down, but I don't know if that is actually possible, or if it is poor programming style.

View Replies View Related

Game Coding - No Enclosing Instance Of Type Background Is Accessible

Dec 11, 2014

So I'm fairly new to java and have been learning game development. I've come across this error message on my StartingClass.java coding. Here is the Coding

@Override
public void start() {
bg1 = new Background(0,0); // This line is giving me the error!!!!!!!!!!!
bg2 = new Background(2160, 0);
hb = new Heliboy(340, 360);
hb2 = new Heliboy(700, 360);
robot = new Robot();

Thread thread = new Thread(this);
thread.start();

The error reads "No enclosing instance of type BackGround is accessible. Must qualify the allocation with an enclosing instance of type BackGround (e.g. x.new A() where x is an instance of BackGround)." Now I have seven classes in my in my games src folder and none of them but the StartingClass.java has the error. Once again I'm fairly new to java and have not benn able to fix it.

These are the classes{

BackGround.java
Enemy.java
Heliboy.java
package-info.java
Projectile.java
Robot.java
StartingClass.java ( errors in this class )
}

View Replies View Related

Converting Parent Instance To Child Instance?

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

Getting Int Type Cast To Lower Precision Char Type In Parasoft JTEST

Mar 11, 2014

Below code I am using to typecast int to char.

char escapeValue = (char)0;
char cha = (char) 10;
escapeValue = (char)(escapeValue * cha); // Here am getting the above error.

I have 38 similar issues in my workspace.

View Replies View Related

Is Type Irrelevant As Long As Value Is Within Boundaries Of Type Of Switch Expression

Apr 30, 2014

If you have final int i = 1;
short s = 1;
switch(s) {
case i: System.out.println(i);
}

it runs fine. Note that the switch expression is of type short (2 bytes) and the case constant is of type int (4 bytes).My question is: Is the type irrelevant as long as the value is within the boundaries of the type of the switch expression?I have the feeling that this is true since:

byte b = 127;
final int i = 127;
switch(b) {
case i: System.out.println(i);
}

This runs fine again, but if I change the literal assigned to i to 128, which is out of range for type byte, then the compiler complains.Is it true that in the first example the short variable and in the second example the byte variable (the switch expressions) are first implicitly converted to an int and then compared with the case constants?

View Replies View Related

How To Convert Input Array Of Type Strings To Class Type

Aug 14, 2014

class Passenger{
String name;
int age;
char gender;
int weight;
public Passenger(){

[Code] ....

This is showing error.....error it gives isthat it cannot change from string type to passenger type......... How to do it??????

View Replies View Related

Addition Of Generic Type Parameter Causes Member Type Clash

Apr 22, 2014

Got a problem with generics, which I'm still pretty new at. Here's a program that compiles fine:

import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();

[Code] ....

It's useless, but it compiles. If I change Line 14, however, to add a generic type parameter to the ListHolder class, Line 10 no longer compiles:

import java.util.ArrayList;
import javax.swing.JComponent;
public class Experiments {
public static void main(String[] args) {
ListHolder holder = new ListHolder();

[Code] ....

I get this error:

Uncompilable source code - incompatible types: java.lang.Object cannot be converted to javax.swing.JComponent
at experiments.Experiments.main(Experiments.java:10)

Apparently, the introduction of the type parameter leaves the compiler thinking that aList is of type Object. I can cast it, like this:

JComponent c = ((ArrayList<JComponent>)holder.aList).iterator().next();

That makes the compiler happy, but why is it necessary? How does adding the (unused) type parameter to the ListHolder class end up making the compiler think the aList member of an instance of ListHolder is of type Object?

View Replies View Related

How To Convert String Type To Generic Class Type

Mar 22, 2015

I have a String repersentaion of some Object.I want that object convert back into some class type how can I do this?

View Replies View Related

How To Convert ZipEntry Type To File Type

Dec 4, 2014

I'm trying to parse and compare the content of a zip file. However I'm stuck at what SHOULD be a very simple problem, however I can't seem to find a solution. I have done the following:

ZipInputStream zin1 = new ZipInputStream(fin);
ZipEntry ze1 = null;
fin2 = new FileInputStream(fileName2);
ZipInputStream zin2 = new ZipInputStream(fin2);
ZipEntry ze2 = null;
//fin.close();
ze1 = zin1.getNextEntry();
ze2 = zin2.getNextEntry();

Which gives me the first entry of each zipfile as a ZipEntry type object. I have tried getting the path of the file (inside the zip file) and using this to create a File type object. This does not seem to work though I get:

Exception in thread "main" java.io.FileNotFoundException: My DocumentsmetadatacoreProperties.xml (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)

And this is because I get a null return from trying to create the File file1 = new File(correctLocation);

I guess I cannot access the file inside a zip file this way. So my question is how can I make a ZipEntry type object into a File type object?

View Replies View Related

Type Of Expression Must Be Array Type

Dec 30, 2014

The objective of the code is to add new records based on existing records with a partial change to the key. I'm getting "type of the expression must be an array type but it resolved to DstidArray" on dsTidRecTbl[i]

String stMajor = request.getParameter("stMajorVersion");
String stMinor = request.getParameter("stMinorVersion");
String stPatch = request.getParameter("stPatchVersion");
StringBuffer stKeySB = new StringBuffer(stMajor+stMinor+stPatch);
String stKey = new String(stKeySB.toString());
DstidArray dsTidRecTbl = new DstidArray(stKey);
request.setAttribute("dsTidRecTbl", dsTidRecTbl);

[code]....

View Replies View Related

Null Is What Type Of Data Type

Sep 3, 2007

method passing null to the called function, compiler would take that parmer as what type of paramer???

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved