12 Replies - 705 Views - Last Post: 06 January 2013 - 09:19 PM Rate Topic: -----

#1 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Problem creating an instance(Harder than you think)

Posted 02 January 2013 - 12:07 AM

I understand that creating an instance of a class is very simple which it is, but I do not understand why I am having this error. The IDE keeps saying quote, "No enclosing instance of type ClassAndAbove1 is accessible. Must qualify the allocation with an enclosing instance of type ClassAndAbove1 (e.g. x.new A() where x is an instance of ClassAndAbove1)," where ClassAndAbove1 is the project name. Attached is a file with the code so you can pick apart the error if you so choose. The problem occurs at the bottom of the code,please help!

import java.io.*;
import java.util.*;
class gvars
{
	static float debt1;
	static float income1;
	static float tax1;
	static float payofftime1;
	static float interest1;
}
        public class ClassAndAbove1 {
	    public static void main(String[] args) {
		System.out.println("Amount of debt : ");
		float debt;
		Scanner debtfile = new Scanner(System.in);
		debt = debtfile.nextFloat();
		gvars.debt1 = debt;
		System.out.println("Amount of income : ");
		float income;
		Scanner incomefile = new Scanner(System.in);
		income = incomefile.nextFloat();
		gvars.income1 = income;
		System.out.println("Tax rate paid(In PERCENT form) : ");
		float taxrate;
		Scanner taxfile = new Scanner(System.in);
		taxrate = taxfile.nextFloat();
		float rawtax = taxrate/100;
		gvars.tax1 = rawtax;
		System.out.println("Unit of time for debt to be paid of in\n1: Months\n2: Years\n: ");
		int decision;
		Scanner timefile = new Scanner(System.in);
		decision = timefile.nextInt();
		if(decision == 1)
		{
			System.out.println("Enter number of months for debt to paid off in : ");
			float month;
			Scanner monthfile = new Scanner(System.in);
			month = monthfile.nextFloat();
			float payofftime = month/12;
			gvars.payofftime1 = payofftime;
		}
		else if(decision == 2)
		{
			System.out.println("Enter number of years(YEARS ONLY) for debt to be paid off in : ");
			float years;
			Scanner yearfile = new Scanner(System.in);
			years = yearfile.nextFloat();
			System.out.println("Enter number of months in addition to the years for debt to be paid off in : ");
			float decismonth;
			Scanner monthfile = new Scanner(System.in);
			decismonth = monthfile.nextFloat();
			float rawmonth = decismonth/12;
			float payofftime = years+rawmonth;
			gvars.payofftime1 = payofftime;
			}
		System.out.println("Amount of interest paid(In PERCENT) : ");
		float interest;
		Scanner intfile = new Scanner(System.in);
		interest = intfile.nextFloat();
		float  decint = interest/100;
		gvars.interest1 = decint;
		Financials action = new Financials();
	}
	class Financials
	{
		float debt = gvars.debt1;
		float income = gvars.income1;
		float tax = gvars.tax1;
		float payofftime = gvars.payofftime1;
		float interest = gvars.interest1;
	}
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem creating an instance(Harder than you think)

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Problem creating an instance(Harder than you think)

Posted 02 January 2013 - 12:23 AM

You are missing a big part of OOP- encapsulation. Having a class for static variables, then having the Financials class' fields take the values of the static variables in your other class is bad practice. A class models an Object. For example, a Person class should model a single Person. Check out my tutorial Moving Away From Parallel Arrays to read more on OOP and good practices.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem creating an instance(Harder than you think)

Posted 05 January 2013 - 05:33 PM

Indent your code correctly, and it will become obvious

A single .java file can have only one public class so if you have

gvars.java (By the way respect Java conventions, a class name starts with a CAPITAL letter) so if you have the class Gvars defined in Gvars.java then the Class ClassAndAbove1 defined in the same .java cannot be public.

Also completly useless to add the String "Class" to a Class name by convention single class instance use the name of the class with the first letter in lower case so

Class Foo
Foo foo = new Foo();

imagine

Class ClassFoo
ClassFoo classFoo = new ClassFoo();

Imagine if Java engineering had written:

Class ClassJLabel extends ClassJComponent

We would simply over used the "C", "L", "A" and twice as much the "S" key on our keyboard :)
Was This Post Helpful? 2
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Problem creating an instance(Harder than you think)

Posted 05 January 2013 - 06:25 PM

Quote

The IDE keeps saying quote, "No enclosing instance of type ClassAndAbove1 is accessible. Must qualify the allocation with an enclosing instance of type ClassAndAbove1 (e.g. x.new A() where x is an instance of ClassAndAbove1)," where ClassAndAbove1 is the project name.


Try Financials action = new ClassAndAbove1().new Financials();

You should refactor this code to instantiate Financials in a constructor or instance method.

This post has been edited by blackcompe: 05 January 2013 - 06:27 PM

Was This Post Helpful? 3
  • +
  • -

#5 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:23 PM

So then my question is, how do I put Gvars(Thanks for informing me on naming conventions)in a different .Java file and use it in ClassAndAbove1?
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:25 PM

Quote

So then my question is, how do I put Gvars(Thanks for informing me on naming conventions)in a different .Java file and use it in ClassAndAbove1?


Take the class definition and put it in a new file named Gvars.java.
Was This Post Helpful? 0
  • +
  • -

#7 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:33 PM

And will this file literally be called "Gvars.java" or it will only be Gvars but with a .java extension? And will I have to create a different public class in Gvars.java not including the Gvars class?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:34 PM

If you have a .java File named MyClass.java, the only thing in it should be the class definition for MyClass. So in GVars.java, only the GVars class should be there.
Was This Post Helpful? 1
  • +
  • -

#9 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:38 PM

Ok so after I have created this Gvars.java file with the Gvars class, how do I access it Gvars.java in ClassAndAbove1?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:39 PM

You would access it the same way as you have been doing.
Was This Post Helpful? 0
  • +
  • -

#11 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:52 PM

Ok so I made this file for Gvars in Gvars.java but after I do that and change gvars to Gvars in my code, it still doesnt work, it gives me the same error message "No enclosing instance of type ClassAndAbove1 is accessible. Must qualify the allocation with an enclosing instance of type ClassAndAbove1 (e.g. x.new A() where x is an instance of ClassAndAbove1)."
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 08:55 PM

Did you make the correction blackcompe suggested in his first post?
Was This Post Helpful? 1
  • +
  • -

#13 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Problem creating an instance(Harder than you think)

Posted 06 January 2013 - 09:19 PM

Ahhhhh thank you macosxnerd101 and blackcompe!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1