• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

increment by one on an object

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to create a new object but everytime i create a new object i want to increment its name by one

example

i create student6 and the next one should be student7
i don't know how many students there will end up being
i thought i could do something like this


so this would create student7 for example

but i am not sure of the correct syntax for it.
 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is not possible to that because it's array declaration of student you cannot increment in declaration
You can do like this
Creat a variable count as static in class
And put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created
May it is firstname, last name which you mentioned in object creation as argument to the constructor
Firstname++, lastname++ put in constructor so it will increment the variable
 
Doug Xander
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:
is not possible to that because it's array declaration of student you cannot increment in declaration
You can do like this
Creat a variable count as static in class
And put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created
May it is firstname, last name which you mentioned in object creation as argument to the constructor
Firstname++, lastname++ put in constructor so it will increment the variable




would you be able to give me an example of how the code would look like for that? here is my Student class
 
Rancher
Posts: 137
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do that in Java.

You might want to check out this post on StackOverflow.
 
Doug Xander
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:You can't do that in Java.

You might want to check out this post on StackOverflow.



i have read that, i am still really confused about how to achieve this in java.

my application has 5 students already created as object and the user menu needs to give the user the option of adding more.
so i ask them to set the variables using scanner (id, firstname, lastname) and then make new Student object out of that..but the issue here is the name of that student object needs to change each time?

I know i should be using an array to do this, but i am having a hard time understanding how to use an array to create an object.
 
Marshal
Posts: 65108
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Xander wrote:. . . i create student6 and the next one should be student7 . . .

You cannot create student6 or student7 programmatically. You cannot create a variable name like that. You can only write variable names directly into the source code.
You can create an array, as you had before:-Try that code on JShell. Write jshell at a terminal/command line and copy the Student class onto JShell. Then write the code above, with or without the comments. If you simply write array, JShell will show you the state of the array.
JShell is designed for people to try out little bits of code and see how they work. Write /exit or /edit Student in JShell; I think you should be able to guess what they mean.
Remember this about arrays:-
  • 1: They have a fixed size; you cannot change the size of my array from 10. You would have to create a new array to get a different size.
  • 2: They are filled with default values when you create them. In the case of arrays of any reference type, with nulls. I think I have told you before to beware of nulls.
  • 3: The index is how far you have gone from the beginning to find the element you want. So the first element is zero distance from the beginning = index 0. The last index in a 10‑element array is 9.
  •  
    Rancher
    Posts: 4190
    47
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's what a List is for.
    Have you learned any of the Collections classes (eg ArrayList) or are you stuck with just arrays?
     
    Doug Xander
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Doug Xander wrote:. . . i create student6 and the next one should be student7 . . .

    You cannot create student6 or student7 programmatically. You cannot create a variable name like that. You can only write variable names directly into the source code.
    You can create an array, as you had before:-Try that code on JShell. Write jshell at a terminal/command line and copy the Student class onto JShell. Then write the code above, with or without the comments. If you simply write array, JShell will show you the state of the array.
    JShell is designed for people to try out little bits of code and see how they work. Write /exit or /edit Student in JShell; I think you should be able to guess what they mean.
    Remember this about arrays:-
  • 1: They have a fixed size; you cannot change the size of my array from 10. You would have to create a new array to get a different size.
  • 2: They are filled with default values when you create them. In the case of arrays of any reference type, with nulls. I think I have told you before to beware of nulls.
  • 3: The index is how far you have gone from the beginning to find the element you want. So the first element is zero distance from the beginning = index 0. The last index in a 10‑element array is 9.


  • this is helpful. thank you i will give this a shot.
     
    Doug Xander
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:That's what a List is for.
    Have you learned any of the Collections classes (eg ArrayList) or are you stuck with just arrays?



    yes i am actually creating all student objects and adding to an array list. would the code look similar to above?
     
    Brecht Geeraerts
    Rancher
    Posts: 137
    7
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    sohail hussain wrote:... Creat a variable count as static in class and put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created...


    It could look like this:

     
    Doug Xander
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Brecht Geeraerts wrote:

    sohail hussain wrote:... Creat a variable count as static in class and put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created...


    It could look like this:



    interesting solution too, thanks. this gives me some ideas to work with on how to solve this problem.
     
    Dave Tolls
    Rancher
    Posts: 4190
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Doug Xander wrote:
    yes i am actually creating all student objects and adding to an array list. would the code look similar to above?



    That's what I would do, unless there's some other requirement I'm missing.

    So you would have a List<Student>, which contains all your Student objects.
    When the user adds a new Student, you would create a new Student and add them to the List.

    Forget about the naming of the Student variable.
    It would be irrelevant to how it gets stored in the application.

    Where that List lives depends on your application as a whole.

    It needs to be accessible to different parts of the app, but that does not mean making it static and "global".
     
    Campbell Ritchie
    Marshal
    Posts: 65108
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    sohail hussain wrote:. . . is not possible to that because it's array declaration of student you cannot increment in declaration . . .

    Where on earth did you get that from? You are not declaring the array there; in order for that code to compile, the array must have been declared already. I have just run that sort of code myself.

    Firstname++, lastname++ put in constructor so it will increment the variable

    How on earth are you going to get firstName++ to compile? The operand for ++ has to be a

    variable of a type that is convertible (§5.1.8) to a numeric type

    (from the Java® Language Specification =JLS).
    It is possible for a class to have its own nextId field, like this:-That is a pat‍tern of code often shown to beginners, but I think I prefer DX's solution supplying the ID from elsewhere.

    Brecht Geeraerts wrote:. . . You might want to check out this post on StackOverflow.

    That also tells us it isn't possible to create variable names on the fly, but becomes complicated after a few replies because people start confusing things by talking about reflection.

    DX: Yes, you can create an array. You can create a count variable, and whenever you add a new Student object, you increment count. But, you can go through the Arrays class looking for methods. The nearest you could get to changing the size of an array is something like this:-Removing an element from an array is awkward. You can delete the last element like this:-If you aren't using the last element you can shorten that code to:-If you remove an element other than the last element, you will have to do something like this:-
  • 1: Record the element you are removing. Only actually necessary if you are going to do something with that element.
  • 2: Shuffle all the subsequent elements one up to fill up the empty space.
  • 3: Set the last array element to null, otherwise you will have two references to it.
  • 4: Decrement count.
  • Maybe you can replace step 2 by System#arraycopy(), if you can understand arraycopy().
     
    Doug Xander
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:

    Doug Xander wrote:
    yes i am actually creating all student objects and adding to an array list. would the code look similar to above?



    That's what I would do, unless there's some other requirement I'm missing.

    So you would have a List<Student>, which contains all your Student objects.
    When the user adds a new Student, you would create a new Student and add them to the List.

    Forget about the naming of the Student variable.
    It would be irrelevant to how it gets stored in the application.

    Where that List lives depends on your application as a whole.

    It needs to be accessible to different parts of the app, but that does not mean making it static and "global".



    i didn't actually realize you could call methods on an arraylist. so i guess the name is irrelevant, that is where i was going wrong. that is what was confusing me.

    thank you

    this works for me making things a lot simplier

     
    Doug Xander
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    as for the adding of new students i can probably create a for loop with logic that will add it.
     
    Campbell Ritchie
    Marshal
    Posts: 65108
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you know how to use Lists, do so. Forget about arrays. A List will add objects in the right place (usually last), will increase its capacity as required, will maintain a record of its size, and will do all the awkward work about removing elements for you without your seeing it.

    Please don't quote the whole of old posts; that simply duplicates writing without adding any new information.
     
    Marshal
    Posts: 6974
    471
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Brecht Geeraerts wrote:

    sohail hussain wrote:... Creat a variable count as static in class and put increment count++ in constructor, so when ever object is created constructor is executed so count variable will incremented by this you can count the number of object created...


    It could look like this:


    I think to have counter in a student class isn't a good idea. Simply misassigned responsibility.

    How the IDs are ensured to be unique? Maybe ID could be re-used for similar purpose?
     
    Campbell Ritchie
    Marshal
    Posts: 65108
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sohail Hussain, I am sorry that I misread your code this morning and was mistaken about the declaration part of your post. Sorry.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!