Why am I Accessing Private Variables

Hi,

I’m learning to program with Processing and a question came to me !
I created a class called Estrela (Star) and I created some private attributes in it, but I’m noticing that I can access these private variables from my main file … Is this normal?

//The Estrela class

class Estrela
{
  private float x,y;
  private float speed;
  private color cor;
  Estrela(float x, float y, float speed, color cor)
  {
    this.x = x;
    this.y = y;
    this.speed = speed;
    this.cor = cor;
  }
  
  void Draw()
  {    
    strokeWeight(3);
    stroke(cor);
    point(x, y);
    x = x - speed;
    if(x < 0)
    {
      x = width;
    }
  }
}

//the main file

import processing.sound.*;
color[] corEstrelas = {#626262, #898787, #CBC9C9, #ffffff };
Estrela[] estrelas;

void setup()
{
  size(800,600);
 inicializaEstrelas();
  
}

void draw()
{
  clear();
  background(0);
  for(Estrela estrela: estrelas)
  {
    //a test -> Why can I acess this private variable of Estrela instance
    estrela.x = 10;// <-- x is a private variable but this give no error ??!! 
    //---------------------
    
    estrela.Draw();
    
  }
}

void inicializaEstrelas()
{
   estrelas= new Estrela[600];
  for(int i=0;i<estrelas.length;i++)
  {
    int x = int(random(0,width));
    int y = int(random(0,height));
    float speed = random(0.5f,5);
    color cor = corEstrelas[int(random(0,corEstrelas.length))];
    //int x, int y, float speed, color cor
    estrelas[i] = new Estrela(x,y,speed,cor);
  }
}

Hi! Welcome to the forum!

yes it’s weird :slight_smile: I know a bit of background but I had to do a bit more research to fully understand the logic. Basically, your code is first compiled to a single Java code (I generated it by processing-java command) :

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class discourse29607 extends PApplet {

  int[] corEstrelas = {0xff626262, 0xff898787, 0xffCBC9C9, 0xffffffff };
  Estrela[] estrelas;

  public void setup()
  {
    
  inicializaEstrelas();
    
  }

  public void draw()
  {
    clear();
    background(0);
    for(Estrela estrela: estrelas)
    {
      //a test -> Why can I acess this private variable of Estrela instance
      estrela.x = 10;// <-- x is a private variable but this give no error ??!! 
      //---------------------
      
      estrela.Draw();
      
    }
  }

  public void inicializaEstrelas()
  {
    estrelas= new Estrela[600];
    for(int i=0;i<estrelas.length;i++)
    {
      int x = PApplet.parseInt(random(0,width));
      int y = PApplet.parseInt(random(0,height));
      float speed = random(0.5f,5);
      int cor = corEstrelas[PApplet.parseInt(random(0,corEstrelas.length))];
      //int x, int y, float speed, color cor
      estrelas[i] = new Estrela(x,y,speed,cor);
    }
  }
  class Estrela
  {
    private float x,y;
    private float speed;
    private int cor;
    Estrela(float x, float y, float speed, int cor)
    {
      this.x = x;
      this.y = y;
      this.speed = speed;
      this.cor = cor;
    }
    
    public void Draw()
    {    
      strokeWeight(3);
      stroke(cor);
      point(x, y);
      x = x - speed;
      if(x < 0)
      {
        x = width;
      }
    }
  }
  public void settings() {  size(800,600); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "discourse29607" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}

notice that your class is nested inside PApplet. And it seems like when classes are nested, both inside and outside class have access to private members:

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

You might ask why Processing nest classes. It’s because, for example, strokeWeight and point are members of PApplet, so thanks to nesting, you can use them as if they are global functions. You can also create your class in a new tab as .java file, and then it won’t be nested in PApplet (I suppose). This case you can use private as you expect, but the class won’t have access to functions like strokeWeight because it is outside PApplet.

4 Likes