Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Static inherited variable?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Static inherited variable?

    Hello everyone, this is my first time here, and I would like to ask a question.

    I have a superclass called Entity, which has a function, that will load images into a Image array, if the array doesn't contain anything, this is fine for most of my cases, when the images aren't repeated, however I have a subclass of Entity named Tree, every single image of the tree is the same, so I don't want each object to load its own image of a tree, I would like the array to be static for the subclasses of Entity, and to not be static for Entity itself.

    I understand there is simple methods around this, such as making a function in each subclass, however, I would like an easier method if that's possible..

    Here is the code, to help you get the idea of what i'm looking for.

    	public BufferedImage getImage(String s)
    	{
    		BufferedImage sprites = null;
    		try {
    			System.out.println("Getting image: "+s+".png");
    			sprites = ImageIO.read(getClass().getResource(s+".png"));
    		} catch (Exception e) {
    			System.out.println(e.getLocalizedMessage());
    		}
    		return sprites;
    	}
     
    	public void loadImages(int xAmount, int yAmount) {
    		BufferedImage sprites = getImage(imagePath+imageName);
    		if (sprites != null) {
    			for (int x = 0; x < xAmount*yAmount; x++) {
    				images[x] = sprites.getSubimage((x%xAmount)*imageWidth, (x/xAmount)*imageHeight, imageWidth, imageHeight);
    			}
    		} else System.out.println("Null Image");
    	}
     
    	public Image getImage() {
    		if (this.images[0] == null) {
    			this.loadImages(imagex, imagey);
    		}
    }

    For each 'unique' entity, it will output "Getting image: whatevertheimageis", and that's fine, but for repeated images, such as Tree's, It will just spam the console with "Getting image: /images/entities/tree.png".

    I hope you understand my question, I would really appreciate some light on this topic! Thankyou very much.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Static inherited variable?

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    In Java, 'functions' are called methods. Please try to communicate in sentences that clearly communicate the requirements you are trying to code.

    I would like the array to be static for the subclasses of Entity, and to not be static for Entity itself.
    I think you can accomplish this by creating a static array in a subclass of Entity and then subclass the new class. With some additional code, the parent class Entity could have access to the same array, but I'm not sure it's needed or worth the trouble of keeping it up to date (synchronized).

    In the end, I think your imagined design can be simplified to overcome the problems you anticipate, but it is difficult to understand your question, since the requirements are not clearly stated.

Similar Threads

  1. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 4
    Last Post: July 19th, 2013, 09:06 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM

Tags for this Thread