Results 1 to 9 of 9

Thread: :confused: Class Help

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    :confused: Class Help

    I have a program that looks like:

    public class tester
    {
    public VPixel asdf;
    ...
    }

    class VPixel
    {
    public VPigment r;
    public VPigment g;
    public VPigment b;
    ...
    }

    class VPigment
    {
    ...
    }


    but it gives me a compile error when i try to use VPixel as a variable in the tester class. Any ideas?
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  2. #2
    You cannot include more than one class in a file. Otherwise JavaC gets confused and gives an error.

  3. #3

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    so how would i reference a class from another file?
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Im not too sure about having seperate classes in one file,
    but you can have nested classes or classes local to a method block "Local class" within one file.



    public class NestedTest{
    public static void main(String[] main){
    Nest n = new Nest(100,500);
    n.z();
    }

    static class Nest{
    static int i;
    int x;
    Nest(int i, int x){
    this.i = i;
    this.x = x;
    }

    // non static variable can access bolth field members
    // static void z(){ then you can only access int i.

    void z(){
    System.out.println(" The value of i is " + i);
    System.out.println(" The value of x is " + x);
    }
    }
    }

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Classes declared without a package statement fall into the defualt package. Then all you have to do is reference them accordingly. If the package statement is not used you have to reference a class with it's full package name. For instance.

    Code:
        import java.util.Vector;
             //or 
        import java.util.*;
    
     public class vectorTest{
          public static void main(String[] args){
           Vector v = new Vector(); 
    
        // if all of the important statements were excluded you 
       //would have to reference Vector as such.
    
          java.util.Vector v = new java.util.Vector(); 
        
      }
    }

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Dilenger4
    Im not too sure about having seperate classes in one file
    The answer to that is Yes, you can have more than one class in one file, but only 1 public class. I believe that separate .class files are made for each class.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    But you cannot create a private class. So any other scope would work.

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yes your right..... You can have more that one class within a
    file. I tend to forget these things. *~sigh
    Luckly i have filbert to remind me.

    I was just fooling around posting code and VbWorld interpted
    the semicolon ) in the code as


    Code:
    //import java.util.*; 
       
    public class MyClass{
      public static void main(String[] args){
    
        java.util.LinkedList ll = new java.util.LinkedList(); 
        String greeting = "Hello my name is";
        String Invitation = "Would you like to goto.....";
        java.util.StringTokenizer st1 = new java.util.StringTokenizer(greeting);
        java.util.StringTokenizer st2 =  new java.util.StringTokenizer(Invitation);
    
        while(st1.hasMoreTokens()){ // tokenize the String
           System.out.print(st1.nextToken());
            System.out.print('\t');
        }
        System.out.println(); 
           
        // add the tokens to a TreeSet
        while(st2.hasMoreTokens()){
           ll.add(st2.nextToken());
        }  
      
       // iterate through the LinkedList
         for(java.util.Iterator i = ll.iterator();i.hasNext();){
           System.out.print(i.next());
             System.out.print('\t');   
         }
       }
     }

  9. #9
    VirtuallyVB
    Guest

    Question

    crptcblade is correct.

    CaptainPinko,
    Your posted code seems fine. How are you trying to "use VPixel as a variable in the tester class"? Can you post the problem code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width