Page 1 of 1

creating objects in java.??

PostPosted: Wed Jun 10, 2015 1:06 pm
by 80HD
ok so every time i go to create an object, i get a syntax error that says " x can not be resolved to a type. for example, my line says
Sniper s1;
"Sniper s1 = new Sniper(); "

i might be doing something wrong. can anyone explain how to make objects?

Re: creating objects in java.??

PostPosted: Wed Jun 10, 2015 4:07 pm
by ghost107
For future references try to add your code in the [code][/code] tags, looks much nicer.

In what I saw you first declare the variable s1(Sniper s1;), then you declare and instantiate it at declaration(Sniper s1 = new Sniper();), which will result in a syntax error because you cannot declare the same variable twice. Also why is "Sniper s1 = new Sniper(); " in quotes?

What you need to do, is by either instantiate it at declaration of the variable, or declare the variable first and later instantiate it.
Creating Objects:
Code: Select all
// Instantiate the object at declaration
Sniper s1 = new Sniper();

or

// Declare object  now and instantiate the object later
Sniper s2;
s2 = new Sniper();


Don't know if you saw this post, but this is a tutorial for java(for NetBeans or Eclipse):
viewtopic.php?f=36&t=11600#p88354

Re: creating objects in java.??

PostPosted: Wed Jun 10, 2015 5:31 pm
by 80HD
thank you very much for you're reply. I see now that I had the wrong layout for objects.but wait, i just tried to plug that in, and im still getting the "Sniper can not be resolved to a type" error.

I will definitely start using those tags btw!

Re: creating objects in java.??

PostPosted: Wed Jun 10, 2015 6:11 pm
by ghost107
This means you didn't declare the class Sniper, you forgot to import the package where the class Sniper is located, or you forgot to add your package declaration.

Code: Select all
Sniper.java:
package my.sniper.package;
public class Sniper {
  ...
}

main.java:
package my.main;
import  my.sniper.package.Sniper;
public class Main{
   public static void main(String[] args){
       Sniper s1 =  new Sniper();

       Sniper s2 = null;
       s2 = new Sniper();
   }
}


Re: creating objects in java.??

PostPosted: Thu Jun 11, 2015 2:49 pm
by 80HD
got itttttt..thank you very much!

Re: creating objects in java.??

PostPosted: Sat Aug 01, 2015 5:31 pm
by ghostheadx2
Adding onto ghost107's solution, classes are just like formats for objects. You can think of an object as the exact object and the class as the category. For example the class would "furniture" and and object would be "John's chair." Or another example, the class is movies and the object is an individual copy of Taken 2, as opposed to someone else's copy.