0

I am trying to initialise the array C in the loop, but it gives error :
C is array of class ipdata and I have declared it and trying to initialise it inside the loop.

import java.io.*;
import java.util.*;
public class cluster_anlysis {
    class ipdata{


        float a,b;
        int cluster;

        ipdata()
        {
            a=0;
        }
    }
    public float modc(float a){
            if(a<0.0)
                a=-a;
            return a;
    }
    public static void main(String[] args) {
        cluster_anlysis obj=new cluster_anlysis();
    ipdata C[] = new ipdata[50];
    float mean1,mean2,mean3;
    int i,j,n1=0,n2=0,n3=0,flag=0;
    float ina=0.0f;
    float inb=0.0f;

    //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Scanner scan =new Scanner(System.in);
    System.out.println("pls enter no of data: ");
    Integer no = scan.nextInt();
    System.out.println("\nnow enter the x and y values");
    for(i=0;i<no;i++)
    {
        ina=scan.nextFloat();
        inb=scan.nextFloat();
        System.out.println(ina);
        C[i]= new ipdata();          // this line is giving error

        C[i].a=ina;
        C[i].b=inb;
        C[i].cluster=0;
    }


    }

}

What could be the problem ? it says : No enclosing instance of type cluster_anlysis is accessible. Must qualify the allocation with an enclosing instance of type cluster_anlysis (e.g. x.new A() where x is an instance of cluster_anlysis).

Anubha
  • 1,345
  • 6
  • 23
  • 35

4 Answers4

4

you create an instance of inner class from static methods of your outer class using the outer class instance.

c[i]= new cluster_anlysis().new ipdata();

as you have alread created cluster_anlysis instance in your first line of main method.

cluster_anlysis obj = new Cluster_anlysis();

you could simple do.

c[i]= obj.new ipdata();

read about Inner classes in java

but if you want to create an inner class instance from the non-static methods of your outer class you dont need the instance of your Outer Class .

 public class OuterClass {
       public void method(){
           InnerClass inner = new InnerClass();
       }
       class InnerClass{
         }
    }

and also follow class name convention as posted by @A.R.S and @JB Nizet in their solutions.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

It seems you're starting with Java. I would advise to avoid messing with inner classes for the moment. Define each Java class in its own file, and everything will be simpler.

Also learn about Java naming conventions:

  • classes start with an upper-case letter, and are CamelCased: ClusterAnalysis, IpData. They should not contain underscores.
  • variables start with a lower-case letter, and are camelCased: c. They should not contain underscores.

Once you are more comfortable with the basics, learn about inner classes and static inner classes in the Java tutorial. But inner classes should not be abused. The basic rule is to have one class per file.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I think the best thing to do would be to declare ipdata as static; that would eliminate the error. There should be no need to instantiate cluster_analysis in order to instantiate ipdata in this case, and it makes sense that ipdata should in fact be static.


Oh, and by convention, you might want to consider using the names IpData and ClusterAnalysis instead - class names generally start with a capital letter and are "CamelCased", as another answer also pointed out.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

Use outer class Instance to call inner class constructor

like

c[i]=new cluster_analysis().new ipdata();
Abhishekkumar
  • 1,102
  • 8
  • 24