Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Warning Question  (Read 931 times)
0 Members and 1 Guest are viewing this topic.
Offline Rocketalypse

Senior Newbie




Bass Slapping Game Dev


« Posted 2012-09-22 11:42:13 »

Hello, JGO community.

When I write (Java) applications, I really don't like having any warnings.
Right now, I am working on a simple Breakout game and I decided to use
Netbeans. Basically, I have a code that in  Netbeans shows a warning,
while in Eclipse it doesn't. I am wondering why. Here is the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
package game;

import java.awt.Color;
import javax.swing.JPanel;

public class Board extends JPanel{
    public Board(String name){
        init(name);
    }
 
    public void init(String name){
        setName(name);
        setBackground(Color.WHITE);
        setFocusable(true);
    }
}


In NetBeans, there is the warning on this line:
1  
init(name); // WARNING: Overridable method call in constructor.


I understand that this is highly irrelevant, but I just want to know how
can I fix that warning without suppressing the warnings for the constructor.
Offline Ultroman

JGO Knight


Medals: 23
Projects: 1


Snappin' at snizzes since '83


« Reply #1 - Posted 2012-09-22 12:15:23 »

Well. it's just warning you that you are using an overridable method in the constructor, which I suppose means if someone extends your class, they can change the behavior of the constructor if they override the init-method.
I'm not really sure that you can do much about it, but I'd recommend making the init()-method private, as it should only be used by that class to initialize its variables.

Also, why do you pass the name on to init? Why not just set the name in the constructor? It's not likely that you want your program to change its name in the middle of execution anyway. The other calls are sitting fine in the init, though.

- Jonas
Offline princec
« League of Dukes »

JGO Kernel


Medals: 196
Projects: 3


Eh? Who? What? ... Me?


« Reply #2 - Posted 2012-09-22 14:51:58 »

The thing with overrideable method calls in constructors is that the overridden method can access stuff in the superclass that has not yet been initialised which can make for some proper hair-pulling moments.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TimB

Junior Member


Medals: 1
Projects: 1



« Reply #3 - Posted 2012-09-22 15:42:35 »

To fix it do any of:
make the class final
make the method private
make the method final
Offline Rocketalypse

Senior Newbie




Bass Slapping Game Dev


« Reply #4 - Posted 2012-09-22 16:15:03 »

Also, why do you pass the name on to init? Why not just set the name in the constructor? It's not likely that you want your program to change its name in the middle of execution anyway. The other calls are sitting fine in the init, though.
Program's name is stored 'n' set in other class named Frame. That was JPanel's name. For some reason I want to set the name of JPanel. Tongue

Thank you all for showing the interest to help me.
I will most likely break my habbit and leave the warning
there, but I'm not sure yet. Once again, thanks.
Offline Best Username Ever

Junior Member





« Reply #5 - Posted 2012-09-22 17:50:16 »

I wonder if that applies for protected functions, too. Most warnings in Eclipse and NetBeans are generated by the IDE, so you won't necessarily get the same warnings in all developer environments. You can turn individual types of warnings on or off or instruct your IDE to treat certain types of warnings as errors. I would make the function private if you do anything to address the error. (Making it implicitly final and invisible to subclasses.)

You can also do

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
package game;

import java.awt.Color;
import javax.swing.JPanel;

public class Board extends JPanel{
    public Board(String name){
        init(name);
    }
 
-   public void init(String name){
+   private void init(String name){
        setName(name);
        setBackground(Color.WHITE);
        setFocusable(true);
    }

+   public void reinitialize(String name)
+   {
+      init(name);
+   }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.121 seconds with 20 queries.