• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating objects in a java class/interface header  RSS feed

 
Ted Gress
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My compiler is throwing errors when I try to declare a String object and a PriorityBlockingQueue in the header.



And it gives me the error:

Description Resource Path Location Type
Syntax error on token ";", { expected after this token IBaseNPC.java /necrotek3d_v/src/GameInterfaces/NPCs line 30 Java Problem


Also the same thing is happening with my PriorityBlockingQueue:



And it gives this error:

Description Resource Path Location Type
Syntax error on token "static", interface expected after this token MessageManager.java /necrotek3d_v/src/MessageManagement line 64 Java Problem


-Ted
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What types are direct_damage and energy_damage?

What version of Java do you use? Declaring static method in interfaces is permitted since Java 8.
(I assumed the second code snippet is from interface IBaseNPC)

What is reference assigned to messagesQueue.

And the most important thing:
Do not use static variables like this. It is a very bad practice. All of these should be instance variables.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About code snippet 1: You cannot put arbitrary statements, such as assignment statements (lines 11 and 12 of your code) in a class or interface like that. Such statements must be inside the body of a method, constructor or initializer block, not directly in the body of the interface or class.

About code snippet 2: Is that method declared inside a class? In Java, you cannot define top-level methods outside of a class or interface. All methods must be defined inside a class or interface. Maybe you misplaced the { and } of your class or interface, so that the method is now outside of the class or interface.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

What have you been taught about names of interfaces? It is not usual Java® practice to start interface names with an I (that is common in C#, however). Also please change your variable names to camelCaseStartingWithALowerCaseLetter rather than using underscores. The old Sun style guide is still useful about that.
 
Ted Gress
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel:
direct_damage and energy_damage are ints.


Java version is 8 update 77.

The second code snippet is from interface IBaseNPC

I'm not sure what you are asking in regards to messagesQueue, but this is where it is instantiated (in MessageManager.java):



The variables are defined as static because they need to be accessed across several dozen classes and passing variable instances doesn't make sense.

Jesper: I'm pretty sure you can do that can't you? The compiler doesn't throw an error typically when I do it and I'm pretty sure you are allowed to instantiate a variable
in a class body without it being inside a method. I know you can in C++ and from what I remember you can declare a variable in a class body in Java.

2. The method is declared inside a class.

Campbell: The code is being modeled after a C# application so it was natural to put the I in the front, plus I think it helps differentiate between classes and interfaces
when coding. The underscores will be changed - I was originally a C programmer so the use of underscores is an outdated habit.

So what's the problem?

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Gress wrote:. . . The code is being modeled after a C# application so it was natural to put the I in the front, plus I think it helps differentiate between classes and interfaces when coding. . . .
Hmmmmmmmmmmmmmmm
So what's the problem?
Apart from those fields being global constants, you mean?

What is the status of fields in interfaces in C#? Do they differ from those in Java®? Can you specify names of variables from interfaces?
 
Ted Gress
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are saying you can't instantiate a variable inside a class unless its in a method or....?
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can instantiate a variable inside a method, or inside an initializer (both static and instance initializer). You can also instantiate a variable as part of a declaration, and declarations can be placed outside of methods and initializers.

(By "instantiate a variable" I mean "assign a value to a variable" when I say that.)

That should agree with what Jesper said earlier, does it clarify your question?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!