Java question reguarding creating classes, instances, and referencing variable in said instance | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java question reguarding creating classes, instances, and referencing variable in said instance

I cannot for the life of me get this program running.I have had to heavily edit alot of the print and variables since most of that information is copyrighted. I have been playignwith Java for about 2 weeks, have been writing this program for about a week, and I cannot get the sstupid thing to reference variables in declared class instances.

15th Jun 2018, 4:54 PM
Kevin
7 Answers
+ 1
so Arun you are saying that I should drop all of the "rolls" as methods inside of classtest and classtest2 definitions so that when I declare the instances of those classes I can reference the rolls as methods using the variables inherited by the instances from the classes?
28th Jun 2018, 1:38 PM
Kevin
+ 2
what are u doing Kevin you really like to play.. but pls be serious about it.. .. PrintCharacter(); is functions of which class. and NCR(); CR(); and why you create a object of classtest class if you are not useing it. remember these point.. static data members and members function . at same class access directly can be access anywhere.. at other class. access with class name. can be access anywhere. instances data members and members function . at same class access at instances function only(directly). static function (with the help of object only) other class access anywhere with the help of object only
15th Jun 2018, 8:23 PM
Arun Tomar
Arun Tomar - avatar
+ 1
my problem (apart from aparently not quite getting Java yet) is that every tutorial I look at says establish a class and define what is in the class (variables, methods, etc...). then once the class is defined I should easily be able to pass variable values in and out of the instances by just typing "instance name"."Variable name" and from there I can use them in methods in other casses as needed....this progeam is basically designed to create and instance of a character with a ton of variables in it. then to calculate the rolls that runn off of a roll tree and save those values to the variables...occasionally the roll tree requires input from the user, and then at the end of the program, it is supposed to print the "character" showing all the values of all the variables. Now, again as I stated before, I have gotten this program to run just fine when I create it linearly and type out each instance of the rolls and looping them back till the program is supposed to move to the next roll, the probem with this
28th Jun 2018, 1:44 PM
Kevin
+ 1
is that the program can only create one character at a time, and its ugly and clunky to look at...and easy example is there is a point where the user decide if they wwant to roll against tree 1 or tree 2. this decision would be represented by a method(in my mind) of the main class and I should olny have to write the decision code 1 time,then when I need it call it with the method name. the problem is when I try to call methods, or variables of classes I have declared, the program doesnt see the variable, even though by every tutorial I have looked at it should be able to. so it is irritating the heck out of me.
28th Jun 2018, 1:48 PM
Kevin
+ 1
This is the summary of the program: define class "Character" (variables that belong to character) define class "Rolls" (Varaibles and methods that beong to roll) main: select how many characters to create. create X instances of characters run thruough the creation rolls of each character getting user input where needed storing the value of each roll iteration into its proper variable in the correct character instance. print on screen, the values of each character instance so that they may be copied onto a character sheet and then the character build can be completed in the real world. for this program to work...I must be able to pass variable values to the roll methods from the character instances and vice versa...my issue is the program seems to be in refusal to see the variables that should be inherited from the class by the instance created...as such I can not reference them....I also cant post the whole code because SL wont let me.
28th Jun 2018, 1:55 PM
Kevin
0
this is the code I am using, it runs perfectly when all variables are decared within the program and I C&P code for every time a dicision must be made. As soon as I start trying to make classes however the program loses reference no matter what I do with the variable name. the program acts like it doesnt even see the class instance. I am coding on N-IDE with my tablet PLEASE HELP!!! package forforums; import java.util.Random; import java.util.Scanner; public class forforums { public static void main(String[] args) { classtest test= new classtest(); classtest2 test2 = new classtest2(); System.out.println("BUILD ACTION 1"); CR(); System.out.println("BUILD ACTION 3"); NCR(); System.out.println("BUILD ACTION 3"); FCR(); PrintCharacter(); } public static void FCR() { int CCC = 1; int rollValue = new Random().nextInt(100) + 1; System.out.println(" " + rollValue); switch (CCC) { case 1: if (rollValue == 1) {System.out.println("Invalid, Reroll");CCFCR = CC
15th Jun 2018, 4:57 PM
Kevin
0
first instance of this problem happens at CCFCR variable. I have done classtest.CCFCR which is where the variabe is declared....the classes have no methods, only variable declarations.
15th Jun 2018, 4:58 PM
Kevin