Jump to content
  • Advertisement
Sign in to follow this  
Matthew Melendez

Java Class Constructor Question

This topic is 2508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone find any problems that make arise with this code?
Will there be a problem with an empty constructor like this?

package foo;


import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
BufferedImage image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB);


public foo()
{
//no code here
}
public void setRgb(int x, int y, int rgb)
{
image.setRGB(x,y,rgb);
}
}


I want to be able to create a new foo object like so:

foo object = new foo();


and be able to do this:

object.setRgb(5,5,0x00000000)

Share this post


Link to post
Share on other sites
Advertisement
There won´t be an error. Although it´s not a good programming style.
The better way is to call the constructor of the image in your own constructor like this:

package foo;

import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
BufferedImage image;

public foo()
{
image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB)
}
public void setRgb(int x, int y, int rgb)
{
image.setRGB(x,y,rgb);
}
}


Hope I could help you!

Share this post


Link to post
Share on other sites

There won´t be an error. Although it´s not a good programming style.
The better way is to call the constructor of the image in your own constructor like this:

package foo;

import java.awt.Image.*;
import java.awt.image.BufferedImage;
public class foo
{
BufferedImage image;

public foo()
{
image = new BufferedImage(64,32,BufferedImage.TYPE_INT_ARGB)
}
public void setRgb(int x, int y, int rgb)
{
image.setRGB(x,y,rgb);
}
}


Hope I could help you!


I tired that and the setRgb() method can't find the image object. I think it might be out of scope of the setRgb() method.

Share this post


Link to post
Share on other sites
I haven't used the AWT classes, but in general the setRgb method should be able to use the image object. Can you post any error message which is generated?

Share this post


Link to post
Share on other sites

The better way is to call the constructor of the image in your own constructor like this:
[/quote]
It is about the same. This is stylistic really.


I tired that and the setRgb() method can't find the image object. I think it might be out of scope of the setRgb() method.
[/quote]
Did you leave "image" as a field or did you accidentally make it local to the constructor?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!