Structure in Java

Posted on 2004-10-10
Last Modified: 2010-03-31

     can i create a structure progrmming in java like C++ structure.
Question by:yeskarthikeyan
LVL 37

Expert Comment

ID: 12274269
In java everything is a class
LVL 37

Accepted Solution

zzynx earned 20 total points
ID: 12274273
Do you mean something like this?

public class MyData {
   private int myInt;
   private boolean myBool;
   private long myLong;

   public MyData() {

   // Setters
   public void setMyInt(int i) { myInt = i; }
   public void setMyBool(boolean b) { myBool = b; }
   public void setMyLong(long l) { myLong = l; }

   // Getter
   public int getMyInt() { return myInt; }
   public boolean getMyBool() { return myBool; }
   public long getMyLong() { return myLong; }
LVL 16

Expert Comment

ID: 12274283
Yes, Java is fully support Object Oriented Programming.

look at the zzynx codes, and you should be obvious on the structure in java programming.
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

LVL 37

Expert Comment

ID: 12274286
Then you can

MyData data = new MyData();

List listOfData = new ArrayList();

MyData data2 = new MyData();
LVL 92

Expert Comment

ID: 12274344
public class MyStructure
   public int myInt;
   public boolean myBool;
   public long myLong;

MyStructure x = new MyStructure();
x.myInt = 6;
x.myBool = true;
x.myLong = x.myInt * 100;

LVL 21

Expert Comment

ID: 12275005
C++ is the same way as Java.  A struct is just a class with the default access as public instead of private:
struct myStruct
  int int;
  BOOL myBool;
  long myLong
is the same as
class myStruct
  int int;
  BOOL myBool;
  long myLong;

In fact you can even put methods in a struct in C++.

As for Java, in OOP it is generally not recommended to create classes that have public fields.  This is because the fields are your implementation of the object and it gets difficult to maintain and/or change your class when all of the access is through the public fields.  One exception to this rule is simple classes.

For example, you could imlement a point like this:
class Point
   int x,y
   public Point(int x, int y)
Since it is a smple class and probably won't change and have methods on it, there is no need to make getter/setters for it.

Then for more complex classes they can use Point directly and gain a few milliseconds improvement in speed.

Class Line
   private Point start, end;
   public Line(int x1, int y1, int x2, y2)
       start=new Point(x1, y1)
       end=new Point(x2, y2);

   public Point getStart()
      return start;
   public Point getEnd()
      return end;
   public int getLength()
       return ...//Code to compute length of line
LVL 37

Expert Comment

ID: 12284530
Thanks for accepting.
Can you explain me why the answer only deserves a C-grade?

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now