Home How do I identify an instance of a class by a variable assigned to the instance in Java?
Reply: 0

How do I identify an instance of a class by a variable assigned to the instance in Java?

user1412
1#
user1412 Published in May 25, 2018, 2:36 pm

I am somewhat new to programming in general, and I have been experimenting with procedural generation, more specifically with Strings. As of right now, I have classes for vowels and consonants, I have instanced every letter of the alphabet, and I have written class methods within vowel.java and consonant.java to return the character itself, to return what type of letter the character is, and to add the character to a string. Everything works so far, but I do not know how to identify an instance of each class based on a variable assigned to it. Here is a block of code to make my words a little more clear.

package procgensentence;

public class ProcGenSentence {

    public static void main(String[] args) {
        String product = "";
        //Class Objects
        vowel a = new vowel(1, 'a');
        consonant b = new consonant(2, 'b');
        consonant c = new consonant(3, 'c');
        consonant d = new consonant(4, 'd');
        vowel e  = new vowel(5, 'e');
        consonant f = new consonant(6, 'f');
    }
}

I want to be able to identify each instance later on by the character assigned to it, so that later on when I am testing whether or not the previous character in the String product was a consonant or a vowel, to determine what type the next character should be, I can properly use my .returnType method for the correct character.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302648 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO