Instancing multiple instances of a class

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1596 Views - Last Post: 25 July 2012 - 11:21 AM Rate Topic: -----

#1 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Instancing multiple instances of a class

Posted 25 July 2012 - 09:59 AM

probably a newbie error, but i am to java.

I have a class which i have made, when i call it once it works perfectly. Except the reasoning for creating a seperate class is that i need it to be used for multiple different instances.

I am attempting (right now) to instance the class 3 times, each call i am using is the same so i am going to generalize the code here.

Defining the variable for each class i do:

private static Class variable = new Class(location);


and then i call a method within each of these classes:

variable.create("string");


the problem that i am having is that when i call the method it seems to set all 3 different variables to whatever value the last one was set to.

What am i doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Instancing multiple instances of a class

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:04 AM

Quote

the problem that i am having is that when i call the method it seems to set all 3 different variables to whatever value the last one was set to.


That's what static means. It's not the most transparent piece of terminology, but the "static" keyword" indicates a member which pertains to the class, not to any instance of the class. So if you have a static variable, setting it in one instance changes it for all instances of the class - because no instance owns it.

private [s]static [/s]Class variable = new Class(location);


Delete the static keyword to fix - this will mean that you have to get away from using static methods, I'm afraid.

(btw- it took me a minute to realize that you meant Class as a generic name for "some class or other", and not a reference to the class called "Class" - at least, I hope that's what you mean!)

This post has been edited by jon.kiparsky: 25 July 2012 - 10:04 AM

Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:07 AM

Don't really understand your problem
if create() is static then it can only reference static variable
but that will work

class MyClass {
   String myStr;

   void create(String str) {
      myStr = str;
   }
}

...

   MyClass a = new MyClass();
   MyClass b = new MyClass();
   MyClass c = new MyClass();
   a.create("aaaaa");
   b.create("bb");
   c.create("c");


Was This Post Helpful? 1
  • +
  • -

#4 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:07 AM

that is what i meant =P thank you for the information, I'm new to this language and so far have done quite well on my own.
Was This Post Helpful? 0
  • +
  • -

#5 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:13 AM

ok, my create method nor the Class were never set to static and if i remove the static declaration from the variables i get the error:

Cannot make a static reference to the non-static field

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:20 AM

I think you should probably show us the code at this point, but that error means you're calling a non-static member (ie, a variable) from a static method.
Was This Post Helpful? 1
  • +
  • -

#7 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:26 AM

if just giving you the relevant code cause otherwise youll be here all day:

Main running class:

//defining variables
private static VBOModel modelBit0 = new VBOModel(-0.93f, 7.53f, 0.31f);
	private static VBOModel modelBitTrue = new VBOModel(-0.93f, 7.53f, 0.31f);
	private static VBOModel modelBitFalse = new VBOModel(-0.93f, 7.53f, 0.31f);


//calling create
private static void setUpVBOs() {
    	//Bit0
    	modelBit0.create("res/bit0.obj");
    	//BitTrue
    	modelBitTrue.create("res/bittrue.obj");
    	//BitFalse
    	modelBitFalse.create("res/bitfalse.obj");
    }


//VBOModel class:

package utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.Vector3f;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class VBOModel {
	private static int vboVertexHandle;
	private static int vboNormalHandle;
	private float x, y, z;
	
	private static Model model;
	
	public VBOModel(float x, float y, float z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
	
	public void draw(float r, float g, float B)/> {
		glTranslatef(x, y, z);
		glColor3f(r, g, B)/>;
		glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
		glVertexPointer(3, GL_FLOAT, 0, 0L);
		glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
		glNormalPointer(GL_FLOAT, 0, 0L);
	
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_NORMAL_ARRAY);
	
		glMaterialf(GL_FRONT, GL_SHININESS, 10f);
		glDrawArrays(GL_TRIANGLES, 0, model.faces.size() * 3);
		
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_NORMAL_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}
	
	public void create(String pathToFile) {
		vboVertexHandle = glGenBuffers();
    	vboNormalHandle = glGenBuffers();
    	
    	model = null;
    	try {
    		model = OBJLoader.loadModel(new File(pathToFile));
    	}catch (FileNotFoundException e){
    		e.printStackTrace();
    		cleanUp(true);
    	}catch (IOException e){
    		e.printStackTrace();
    		cleanUp(true);
    	}
    		
    	FloatBuffer vertices = reserveData(model.faces.size() * 9);
    	FloatBuffer normals = reserveData(model.faces.size() * 9);
    		
    	for (Face face : model.faces){
    		vertices.put(asFloat(model.vertices.get((int) face.vertex.x - 1)));
    		vertices.put(asFloat(model.vertices.get((int) face.vertex.y - 1)));
    		vertices.put(asFloat(model.vertices.get((int) face.vertex.z - 1)));
    		
    		normals.put(asFloat(model.normals.get((int) face.normal.x - 1)));
    		normals.put(asFloat(model.normals.get((int) face.normal.y - 1)));
    		normals.put(asFloat(model.normals.get((int) face.normal.z - 1)));
    	}
    	vertices.flip();
    	normals.flip();
    	
    	glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
    	glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
    	glBindBuffer(GL_ARRAY_BUFFER, vboNormalHandle);
    	glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
	}
	
	public void cleanUp(boolean asCrash) {
		glDeleteBuffers(vboVertexHandle);
		glDeleteBuffers(vboNormalHandle);
        Display.destroy();
        System.exit(asCrash ? 1 : 0);
	}
	
	private static FloatBuffer reserveData(int size){
    	FloatBuffer data = BufferUtils.createFloatBuffer(size);
    	return data;
    }
    	
    private static float[] asFloat(Vector3f v){
    	return new float[]{v.x, v.y, v.z};
    }
}


Model Class:

package utility;

import java.util.ArrayList;
import java.util.List;

import org.lwjgl.util.vector.Vector3f;

public class Model {
	public List<Vector3f> vertices = new ArrayList<Vector3f>();
	public List<Vector3f> normals = new ArrayList<Vector3f>();
	public List<Face> faces = new ArrayList<Face>();
}



Face Class:
package utility;

import org.lwjgl.util.vector.Vector3f;

public class Face {
	public Vector3f vertex = new Vector3f();
	public Vector3f normal = new Vector3f();
	
	public Face(Vector3f vertex, Vector3f normal){
		this.vertex = vertex;
		this.normal = normal;
	}
}



that is literally everything related to this issue.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:28 AM

private static void setUpVBOs() {
    	//Bit0
    	modelBit0.create("res/bit0.obj");
    	//BitTrue
    	modelBitTrue.create("res/bittrue.obj");
    	//BitFalse
    	modelBitFalse.create("res/bitfalse.obj");
    }



This is a static method.
Was This Post Helpful? 1
  • +
  • -

#9 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:32 AM

is that whats been doing it? lol wood for trees.

ok i removed the static call from setUpVBOs and now i cannot call it from inside its own class.

Cannot make a static reference to the non-static method setUpVBOs() from the type ProjectBishop

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:32 AM

Remember, you won't be able to run this directly from the main method (which is static by definition) or from any method called directly from main. You'll have to create an object to run it from.

public class Foo
{
  public static void main(String[] args)
  {
    frobnicate();  // fails - frobnicate is not static
    Foo foo = new Foo();
    foo.frobnicate(); // works - now you're calling a method of an object
  }

  public void frobnicate()
  { ; }
}

This post has been edited by jon.kiparsky: 25 July 2012 - 10:34 AM

Was This Post Helpful? 1
  • +
  • -

#11 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:33 AM

i dont understand what you mean by that
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:35 AM

See the edit.
Was This Post Helpful? 1
  • +
  • -

#13 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:46 AM

im still having the exact same problem as before.

the problem is that in the following code it sets all 3 variables to the final input string

private void setUpVBOs() {
    	//Bit0
    	modelBit0.create("res/bit0.obj");
    	//BitTrue
    	modelBitTrue.create("res/bittrue.obj");
    	//BitFalse
    	modelBitFalse.create("res/bitfalse.obj");
    }

Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:51 AM

Quick review:

Java is an object-oriented language. That means that the work is done by objects. Objects contain their own data, and they work on that data. Static methods don't know anything about the data contained by any particular instance. Therefore, you can't reference an instance variable from a static context.
The main method must be static - this is how Java is able to run it before any object exists. This means that you can't call an instance method or reference an instance field from the main method.

The way you get around this is: your main method creates an instance of the object it's in, and either asks it to perform certain actions, ie, calls certain methods, or it calls a "starter" method that it contains (and from that method, certain actions are taken)


Find every place where you call something static, and unless it's the main method, delete "static" and make sure your references are backed by objects.

This post has been edited by jon.kiparsky: 25 July 2012 - 10:52 AM

Was This Post Helpful? 1
  • +
  • -

#15 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 193
  • View blog
  • Posts: 846
  • Joined: 02-November 11

Re: Instancing multiple instances of a class

Posted 25 July 2012 - 10:58 AM

ok, i understand that now, thank you. Except this has not resolved the issue. I am still having the problem that when the VBOModel.create method is called for each of the individual variables however every variable is set to the path passed in by the final one.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2