Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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 1150 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: 24
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

JGO Kernel


Medals: 282
Projects: 3
Exp: 16 years


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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!