creating objects in java.??

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

creating objects in java.??

Post by 80HD on Wed Jun 10, 2015 1:06 pm
([msg=88434]see creating objects in java.??[/msg])

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?
80HD
New User
New User
 
Posts: 6
Joined: Fri Jun 05, 2015 1:10 pm
Blog: View Blog (0)


Re: creating objects in java.??

Post by ghost107 on Wed Jun 10, 2015 4:07 pm
([msg=88438]see Re: creating objects in java.??[/msg])

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
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: creating objects in java.??

Post by 80HD on Wed Jun 10, 2015 5:31 pm
([msg=88443]see Re: creating objects in java.??[/msg])

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!
80HD
New User
New User
 
Posts: 6
Joined: Fri Jun 05, 2015 1:10 pm
Blog: View Blog (0)


Re: creating objects in java.??

Post by ghost107 on Wed Jun 10, 2015 6:11 pm
([msg=88444]see Re: creating objects in java.??[/msg])

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();
   }
}

User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: creating objects in java.??

Post by 80HD on Thu Jun 11, 2015 2:49 pm
([msg=88465]see Re: creating objects in java.??[/msg])

got itttttt..thank you very much!
80HD
New User
New User
 
Posts: 6
Joined: Fri Jun 05, 2015 1:10 pm
Blog: View Blog (0)


Re: creating objects in java.??

Post by ghostheadx2 on Sat Aug 01, 2015 5:31 pm
([msg=89190]see Re: creating objects in java.??[/msg])

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.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests