Very simple JAVA question

Put your problem here if it does not fit any of the other categories.

Very simple JAVA question

Postby mvdgaag » Sat Jul 03, 2010 8:10 pm

Hi all,

I'm a noob to JAVA, so excuse me for my stupidity :)
If I have a function that returns a static instance of my class, why do I have to use an allocation every single time?

Code: Select all
public static A doSomething(int j)
{
  ret = new A(); // allocation!
  // do something
  return ret;
}


I solved it now like this, which is ugly:

Code: Select all
static A ret = new A();

public static A doSomething(int j)
{
  // do something
  return ret;
}

Is there a better/nicer/easier way?

Thanks!

Maarten
mvdgaag
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Jun 09, 2010 10:02 pm

Top

Re: Very simple JAVA question

Postby blundell » Sat Jul 03, 2010 10:14 pm

Do you mean you don't want to instantiate an object of type A?

make A a static class, then whatever your doing with it you can just call:
Code: Select all
A.learnJava();
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Very simple JAVA question

Postby mvdgaag » Sat Jul 03, 2010 10:35 pm

Yes, that's what I mean, but the class itself has static and nonstatic methods and many instances. Some methods return a static instance of the same class though.

I don't want it to allocate a new instance every time my static method is called, which seems to happen when I use new A(). Instead I'd like to just manipulate and return a static object.

Java seems to want me to either use 'new' within the method or instantiate the static outside the method. I think there should be a 'cleaner' way to do this. I just don't know how.
mvdgaag
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Jun 09, 2010 10:02 pm

Re: Very simple JAVA question

Postby blundell » Sat Jul 03, 2010 10:37 pm

If the class has non-static methods it means the class IS NOT static ... therefore you have to instantiate it.
Either separate the class out to make a whole static class or deal with it :-p
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Very simple JAVA question

Postby anarche » Sun Jul 04, 2010 3:56 am

the second way you are doing things is a fairly normal dev pattern (singleton pattern). Be warned that if you use this then each call to each different object of this type will affect the same actual object (the singleton).

If you want to have difference instances of this class (with their own variables etc). Don't use this pattern if you need difference instances of this object running at the same time.

Second Java tip: we call them methods, not functions :d www.sun.com has all you need to know about Java.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Very simple JAVA question

Postby mvdgaag » Sun Jul 04, 2010 10:15 am

thanks anarche. I'm not looking to make a singleton class. It's a 3d vector class that can have many instances.

I just don't like the fact that when I return an int 10000 times a frame I'm most likely just setting a register value, while if I return an instance of my class I get the whole memory allocation and the GC causing hiccups in my game.

So I decided to prevent dynamic allocation as much as possible. To to this some of my methods can manipulate and return a single static instance. It is up to the object that calls the method to copy this instance (if needed) or just use it and leave it. It would become a bit of a problem if multiple threads want to use this static return value though.

I think you guys are right and I should just read up more on java or possibly just the the android NDK and wrap my whole game in one java call :p
mvdgaag
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Jun 09, 2010 10:02 pm

Top

Re: Very simple JAVA question

Postby anarche » Sun Jul 04, 2010 10:23 am

Commended.

A class can be partially static - say the int registry value - and partially instantiated.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 15 guests