Solved

Structure in Java

Posted on 2004-10-10
7
382 Views
Last Modified: 2010-03-31
Hi,

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

Expert Comment

by:zzynx
ID: 12274269
In java everything is a class
0
 
LVL 37

Accepted Solution

by:
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; }
}
0
 
LVL 16

Expert Comment

by:suprapto45
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:zzynx
ID: 12274286
Then you can

MyData data = new MyData();
data.setMyInt(10);
data.setMyBool(false);
data.setMyLong(12300);

List listOfData = new ArrayList();
listOfData.add(data);

MyData data2 = new MyData();
data2.setMyInt(15);
data2.setMyBool(true);
data2.setMyLong(3400);
listOfData.add(data2);
0
 
LVL 92

Expert Comment

by:objects
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;


0
 
LVL 21

Expert Comment

by:MogalManic
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
public:
  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)
   {
      this.x=x;
      this.y=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
    }
}
0
 
LVL 37

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 78
solarwind tftp server 2 44
rhino JavaScript import, load 25 66
why cannot we forward request once the response is committed 2 33
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)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

911 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